Modular Exponentiation

Why most of the test cases I am failing ??

It’s because this is not how modular exponentiation is implemented .
Please refer this link and understand what modular exponentiation is https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/

Ok but if we do (a+b)%mod or (a%mod+b%mod)%mod in both the cases ans will be same then why my code is incorrect ??

Yeah but your answer is incorrect because of the overflow.
pow(a,b) returns garbage value because of integer overflow

https://ide.codingblocks.com/s/224195 please help me to correct this code

Change line 6 to int last_bit=(p&1);
and use long int.
Then your code works fine.