/* Problem URL: https://codeforces.com/gym/105900/problem/F */ #include using namespace std; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } template tuple ext_gcd(T a, T b) { if (!a) return {b, 0, 1}; auto [g, x, y] = ext_gcd(b%a, a); return {g, y - b/a*x, x}; } template struct crt { T a, m; crt() : a(0), m(1) {} crt(T a_, T m_) : a(a_), m(m_) {} crt operator * (crt C) { auto [g, x, y] = ext_gcd(m, C.m); if ((a - C.a) % g) a = -1; if (a == -1 or C.a == -1) return crt(-1, 0); T lcm = m/g*C.m; T ans = a + (x*(C.a-a)/g % (C.m/g))*m; return crt((ans % lcm + lcm) % lcm, lcm); } }; ll tot(int n) { ll ret = n; for (int i = 2; i*i <= n; i++) if (n % i == 0) { while (n % i == 0) n /= i; ret -= ret / i; } if (n > 1) ret -= ret / n; return ret; } ll fastpow(ll a, ll b, ll tot) { ll ans = 1; ll now = a; for (int i = 0; i < 31; i++) { if ((b >> i) & 1) { ans *= now; ans %= tot; } now *= now; now %= tot; } return ans; } vvl mul(vvl &a, vvl &b, ll mod) { vvl ans(a.size(), vl(b[0].size())); rep(i, a.size()) { rep(j, b[0].size()) { rep(k, a[0].size()) { ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % mod; } } } return ans; } ll binaryexpo(ll pow, ll mod) { vvl ans = { {1, 0}, {0, 0} }; if (pow <= 1) { return pow; } pow--; vvl now = { {1, 1}, {1, 0} }; rep(i, 62) { if ((pow >> i) & 1) { ans = mul(ans, now, mod); } now = mul(now, now, mod); } return ans[0][0]; } ll factor(ll n) { ll sqr = sqrt(n); for (ll i = 2; i <= sqr; i++) { if (n % i == 0) { return i; } } return n; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll a, b, m; cin >> a >> b >> m; ll mtot = tot(m); if (__gcd(a, m) != 1) { ll tmp = factor(mtot); crt one(fastpow(a, b, tmp), tmp); crt two(fastpow(a, b, mtot / tmp), mtot / tmp); mtot = (one * two).m; } cout << binaryexpo(fastpow(a, b, mtot), m) << '\n'; }