RSIGNS-Problem with modular exponentiation

I was doing the road signs problem (link :[RSIGNS Problem - CodeChef]).
I got the first 20 points.Then i read the editorial.They were using the modular exponentiation to do the modulo.

I went to geeks for geeks and searched about it.
When i tried to run their code ,i was getting WA verdict for 2nd subtask.
I even tried to use the pow(a,b,c) python library function but the result was same.

This is the link to my code: CodeChef: Practical coding for everyone

I can’t figure out what’s wrong in my code.
I searched other sumbissions and found this : CodeChef: Practical coding for everyone

Is the code on my sumbission for modular exponentiation wrong?