Modular Exponentiation, one test case not passing

All test cases are passing except for the last test case. I took modulo at each step,
I think I am missing some edge case
Link for my solution: https://ide.codingblocks.com/s/78296
Link for the problem: https://hack.codingblocks.com/contests/c/787/743