#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define mod 1000000007
ll mod, m;
ll f(ll a, ll n, ll mod)
{
ll res = a, ans = 0;
while (n)
{
if (n % 2)
ans = (ans + res) % mod;
res = (res + res) % mod;
n /= 2;
}
return ans;
}
ll po(ll a, ll n)
{
ll res = a, ans = 1;
while (n)
{
if (n % 2)
ans = f(ans, res, mod);
res = f(res, res, mod);
n /= 2;
}
return ans;
}
ll aka(ll p, ll alpha)
{
if (alpha == 0)
return 1;
if (alpha == 1)
return (p + 1) % mod;
if (alpha % 2 == 1)
return (f(p, aka(p, alpha - 1), mod) + 1) % mod;
if (alpha % 2 == 0)
return (f((po(p, alpha / 2) + 1), ((aka(p, alpha / 2) - 1 + mod) % mod), mod) + 1) % mod;
}
int main()
{
ll x, n, m;
cin >> x >> n >> m;
mod = m;
cout << aka(x, n);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCi8vICNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKbGwgbW9kLCBtOwpsbCBmKGxsIGEsIGxsIG4sIGxsIG1vZCkKewogICAgICAgIGxsIHJlcyA9IGEsIGFucyA9IDA7CiAgICAgICAgd2hpbGUgKG4pCiAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKG4gJSAyKQogICAgICAgICAgICAgICAgICAgICAgICBhbnMgPSAoYW5zICsgcmVzKSAlIG1vZDsKICAgICAgICAgICAgICAgIHJlcyA9IChyZXMgKyByZXMpICUgbW9kOwogICAgICAgICAgICAgICAgbiAvPSAyOwogICAgICAgIH0KICAgICAgICByZXR1cm4gYW5zOwp9CmxsIHBvKGxsIGEsIGxsIG4pCnsKICAgICAgICBsbCByZXMgPSBhLCBhbnMgPSAxOwogICAgICAgIHdoaWxlIChuKQogICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChuICUgMikKICAgICAgICAgICAgICAgICAgICAgICAgYW5zID0gZihhbnMsIHJlcywgbW9kKTsKICAgICAgICAgICAgICAgIHJlcyA9IGYocmVzLCByZXMsIG1vZCk7CiAgICAgICAgICAgICAgICBuIC89IDI7CiAgICAgICAgfQogICAgICAgIHJldHVybiBhbnM7Cn0KbGwgYWthKGxsIHAsIGxsIGFscGhhKQp7CiAgICAgICAgaWYgKGFscGhhID09IDApCiAgICAgICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICBpZiAoYWxwaGEgPT0gMSkKICAgICAgICAgICAgICAgIHJldHVybiAocCArIDEpICUgbW9kOwogICAgICAgIGlmIChhbHBoYSAlIDIgPT0gMSkKICAgICAgICAgICAgICAgIHJldHVybiAoZihwLCBha2EocCwgYWxwaGEgLSAxKSwgbW9kKSArIDEpICUgbW9kOwogICAgICAgIGlmIChhbHBoYSAlIDIgPT0gMCkKICAgICAgICAgICAgICAgIHJldHVybiAoZigocG8ocCwgYWxwaGEgLyAyKSArIDEpLCAoKGFrYShwLCBhbHBoYSAvIDIpIC0gMSArIG1vZCkgJSBtb2QpLCBtb2QpICsgMSkgJSBtb2Q7Cn0KaW50IG1haW4oKQp7CiAgICAgICAgbGwgeCwgbiwgbTsKICAgICAgICBjaW4gPj4geCA+PiBuID4+IG07CiAgICAgICAgbW9kID0gbTsKICAgICAgICBjb3V0IDw8IGFrYSh4LCBuKTsKfQ==