N = 3233
A = 17
B = 65
R = 0
def quick(a, pow, div): A = 1 T = a % div while (pow != 0): if (pow & 1): A = (A * T) % div pow >>= 1 T = (T * T) % div return A
print(quick(65, 17, 3233)) # 2790