I don't think you've got the second part right.
Taking the example of 5764801 and 17807724.
You get the answers 8 and 11 for the loop sizes.
This because:-
7^8 mod 20201227 = 5764801
7^11 mod 20201227 = 17807724
With this information what you then want to compute is:-
5764801^11 mod 20201227
or:-
17807724^8 mod 20201227
They should be the same number because:-
5764801 = 7^8 mod 20201227 so substituting that in gives:-
= (7^8 mod 20201227)^11 mod 20201227
= (7^8)^11 mod 20201227
= 7^(8*11) mod 20201227
= 7^88 mod 20201227
and likewise:-
17807724^8 mod 20201227
= (7^11 mod 20201227)^8 mod 20201227
= (7^11)^8 mod 20201227
= 7^(11*8) mod 20201227
= 7^88 mod 20201227
which is the same expression as above and equals the example answer of 14897079.
Using the numbers from your input you'll find you have much bigger exponents than 11 and 8.
For the second part you can do it one step at a time, or you can use some maths library to calculate x to the power y mod m. (For this kind of thing I use the libgmp C library and the gmp_pwom() function.)
The step by step approach takes ~4 seconds in my perl solution for both parts. I haven't bothered to implement the second part with libgmp as you still need to do the first step by hand.
There are ways to do the first part faster than brute force, algorithms like
https://en.wikipedia.org/wiki/Baby-step_giant-step, but it's still computationally expensive and the fundamental problem of finding the answer to an equation of the form e^x mod m is called the Discrete Log problem, and the fact it is very difficult to solve is the foundation of many parts of public-key encryption systems.