def fast_mod(x, n, m):
a = 1
b = x
while True:
temp = n
if n % 2 == 1 :
a = a * b % m
b = b * b % m
n = n//2
if temp < 1 :
return a