RSIGNS-Problem with modular exponentiation

I was doing the road signs problem (link :[https://www.codechef.com/problems/RSIGNS]).
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: https://www.codechef.com/viewsolution/27432954

I can’t figure out what’s wrong in my code.
I searched other sumbissions and found this : https://www.codechef.com/viewsolution/26474160

Is the code on my sumbission for modular exponentiation wrong?