HELP Needed in MATBREAK

Here is my Code for MATBREAK.
It is running for the sample test case and some cases I thought by myself.
But it is getting WA. I am not able to figure out where it is failing.
Could somebody help me?

expnt might overflow. You either have to store the product directly (under the modulo, of course) or store the exponent modulo 10^9 + 6 (more detail here)