In powpow2 finding remainder

how remainders r1,r2,r3 are calculated by brute force and by lucas method. please help not able to understand.