import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; // math functions import std.numeric; // gcd void main() { auto rd = readln.split.to!(long[]); writeln(calc(rd[0], rd[1], rd[2])); } auto calc(long a, long n, long m) { if (n == 0) return 1L; if (m == 1) return 0L; auto g = gcd(a, m); if (g == 1) return modPow1(a, calc(a, n - 1, phi(m)), m); auto gp = modPow2(g, a, n - 1, m); if (gp == 0) return 0; return (gp * modPow1(a/g, calc(a, n - 1, phi(m)), m)) % m; } auto phi(long n) { auto r = 0L; foreach (i; 1..n) if (gcd(i, n) == 1) ++r; return r; } auto modPow1(long a, long n, long m) { if (n == 0) return 1L; if (m == 1) return 0L; auto r = 1L, s = a; while (n > 0) { if (n & 1) r = (r * s) % m; s = (s * s) % m; n >>= 1; } return r; } auto modPow2(long g, long a, long n, long m) { if (n == 0) { return g; } else if (n == 1) { if (a * log(g) > m) return 0L; return g ^^ a % m; } else if (n == 2) { if (a * log(a) * log(log(g)) > log(log(m))) return 0L; return g ^^ (a ^^ a) % m; } else { return 0L; } }