#include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair; using vi = vector; template ostream& operator<<(ostream& os, const vector& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template constexpr T INF = numeric_limits::max() / 100; ll A, N, M; vector prime; unordered_map A_factor; template T extgcd(const T a, const T b, T& x, T& y) // ax+by=gcd(a,b) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } ll power(const ll n, const ll mod) { if (n == 0) { return 1; } if (n % 2 == 1) { return (mod != -1) ? ((power(n - 1, mod) * A) % mod) : (power(n - 1, mod) * A); } else { const ll pp = power(n / 2, mod); return (mod != -1) ? ((pp * pp) % mod) : (pp * pp); } } template pair ChineseRemainderTheorem(const pair a1, const pair a2) { assert(gcd(a1.first, a2.first) == 1); const T p1 = a1.first; const T m1 = a1.second; const T p2 = a2.first; const T m2 = a2.second; if (m1 == m2) { return make_pair(p1 * p2, m1); } else { T x, y; extgcd(p1, p2, x, y); assert(p1 * x + p2 * y == 1); x *= (m2 - m1); y *= (m2 - m1); const T p = p1 * p2; return make_pair(p, (((p1 * x + m1) % p) + p) % p); } } ll over(const ll n, const long double x) // A↑↑n >= x なら 0, それ以外なら A↑↑n { if (n == 0) { return (1 >= x) ? 0 : 1; } if (x <= 1) { return 0; } const long double lx = log2(x) / log2(A); const ll p = over(n - 1, lx); if (p == 0) { return 0; } else { return power(p, -1); } } ll rem(const ll n, const ll mod) // A↑↑n (mod m) { if (n == 0) { return 1 % mod; } else if (mod == 1) { return 0; } else { ll num = mod; pair result = make_pair(1, 0); for (const ll p : prime) { if (num % p == 0) { ll beki = 1; ll cnt = 0; while (num % p == 0) { beki *= p; num /= p; cnt++; } const ll phi = beki / p * (p - 1); pair fac = make_pair(beki, 1); if (A_factor.find(p) != A_factor.end()) { const ll ind = over(n - 1, (long double)cnt / A_factor[p]); if (ind == 0) { fac.second = 0; } else { fac.second = power(ind, fac.first); } } else { const ll m = rem(n - 1, phi); fac.second = power(m, fac.first); } result = ChineseRemainderTheorem(result, fac); } if (num == 1) { break; } } return result.second; } } int main() { cin >> A >> N >> M; vector isprime(M + 1, true); for (ll i = 2; i <= M; i++) { if (isprime[i]) { for (ll j = 2; i * j <= M; j++) { isprime[i * j] = false; } } } for (ll i = 2; i <= M; i++) { if (isprime[i]) { prime.push_back(i); } } ll num = A; for (const ll p : prime) { if (num % p == 0) { ll cnt = 0; while (num % p == 0) { cnt++; num /= p; } A_factor[p] = cnt; } } cout << rem(N, M) << endl; return 0; }