Cannot understand solution

Can somebody please explain how this code is working?

Problem Link -> https://www.codechef.com/problems/KBIGNUMB

Solution Link -> https://www.codechef.com/viewsolution/32446060

Won’t work here because N \leq 10^{12}.

The code is working, but I cannot understand how, pls explain if you know