from math import gcd def xgcd(a,b): prevx, nextx = 1, 0 prevy, nexty = 0, 1 while b: quotient = a//b nextx, prevx = prevx - quotient * nextx, nextx nexty, prevy = prevy - quotient * nexty, nexty a, b = b, a % b #print(a,b) return prevx, prevy,a def pfact(m): pf = {} for i in range(2,int(m**0.5)+1): while m%i == 0: pf[i] = pf.get(i,0) + 1 m //= i if m>1 : pf[m]=1 return pf def euler(m): tmp = pfact(m) ans = 1 for i, j in tmp.items(): ans = ans * (i**(j-1)) * (i-1) return ans def perfectdecompose(d, u): for i in range(2,int(d**0.5)+1): while d%i == 0: d//=i while u%i == 0: u//=i if d>1: while u%d == 0: u//=d return u def tetinf(n, u): if u == 1: return 0 d = gcd(n, u) pf = perfectdecompose(d, u) return pow(n, tetinf(n, euler(pf)), pf) * (u//pf) * xgcd(u//pf, pf)[0] % u n,k,m = map(int,input().split()) if k >= 4 and n >= 3: print(tetinf(n, m)) elif n == 2 and k == 4: print(65536 % m) elif k == 3: if n % m == 0 and pow(n, n, m) == 0: print(0) else: print(pow(n, pow(n, n, m), m)) elif k == 2: print(pow(n, n, m)) elif k == 1: print(n % m) else: print(1)