Instead of doing the modular operation in the end, try doing it at each step. If you don’t understand why we are doing this, then first of all, learn modular arithmetic
Se my solution for instance: http://www.codechef.com/viewsolution/4708980
1 Like