getting WA in Maxim and Progressions (June Challenge 2014 )

Question Maxim and Progressions
Can you tell me whats wrong with my solution I am applying the same logic as given in editorial but i am having problem in applying modulus operator. in my fast exponentiation function where should i apply modulus operator so that value does not overflow and applying modulus in exponentiation would’nt change the result ?.

Change this line:

to: ans=(power(2,n)-1-ans + MOD)%MOD;

This should fix your problem, assuming all your other logic is correct. Your fast exponentiation method seems fine to me.

Thanks for your reply. but i dont know why do i get compilation error when i replace my line with your line. And i am sure that fast exponentiation is giving me wrong ans . it is giving me 0 for 2^100.
see the input i have given.

replaced line solution

Original solution