#include <bits/stdc++.h>
using namespace std;
#define sz(o) ((int)o.size())
#define all(o) o.begin(), o.end()
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define repr(i, a, b) for(int i = (a); i >= (b); i--)
#define INF 1000000000000000000LL
#define MOD 1000000007
#define EPS 1e-9
#define PI 3.1415926535
#define ff first
#define ss second
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pi> vpi;
class SimpleMathProblem{
public:
map<int, int> factorize(int n){
map<int, int> res;
for(ll i = 2; i * i <= n; i++){
while(n % i == 0){
res[i]++;
n /= i;
}
}
if(n > 1) res[n]++;
return res;
}
int phi(int n){
map<int, int> factors = factorize(n);
int res = n;
for(auto p : factors){
res *= (p.ff - 1);
res /= p.ff;
}
return res;
}
int bpow(int a, int b, int m){
int res = 1;
while(b > 0){
if(b & 1) res = (int)((res * 1LL * a) % m);
a = (int)((a * 1LL * a) % m);
b >>= 1;
}
return res;
}
int calculate(int a, int b, int c, int m){
map<int, int> factors = factorize(m);
int ans = 0;
vector<pair<int, pi>> v;
for(auto p : factors){
int mod = (int)pow(p.ff, p.ss);
if(a % p.ff != 0){
int x = bpow(b, c, phi(mod));
int ci = bpow(a, x, mod);
int N = m / mod;
int invN = bpow(N, mod-2, mod);
v.push_back({ci, {N, invN}});
}
}
for(auto p : v){
ans += (int)((((p.ff * 1LL * p.ss.ff) % m) * 1LL * p.ss.ss) % m);
ans %= m;
}
return ans;
}
};
void solve(int testcase){
SimpleMathProblem *obj = new SimpleMathProblem();
cout << obj->calculate(3737373, 7373737, 37373, 737373737);
}
int main() {
#ifdef VPAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(20);
clock_t b = clock();
int t = 1;
//cin >> t;
rep(tt, 1, t) solve(tt);
clock_t e = clock();
cerr << (double(e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AgICAgcmV0dXJuIHJlczsKICAgIH0KCiAgICBpbnQgcGhpKGludCBuKXsKICAgICAgICBtYXA8aW50LCBpbnQ+IGZhY3RvcnMgPSBmYWN0b3JpemUobik7CiAgICAgICAgaW50IHJlcyA9IG47CiAgICAgICAgZm9yKGF1dG8gcCA6IGZhY3RvcnMpewogICAgICAgICAgICByZXMgKj0gKHAuZmYgLSAxKTsKICAgICAgICAgICAgcmVzIC89IHAuZmY7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9CgogICAgaW50IGJwb3coaW50IGEsIGludCBiLCBpbnQgbSl7CiAgICAgICAgaW50IHJlcyA9IDE7CiAgICAgICAgd2hpbGUoYiA+IDApewogICAgICAgICAgICBpZihiICYgMSkgcmVzID0gKGludCkoKHJlcyAqIDFMTCAqIGEpICUgbSk7CiAgICAgICAgICAgIGEgPSAoaW50KSgoYSAqIDFMTCAqIGEpICUgbSk7CiAgICAgICAgICAgIGIgPj49IDE7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9CgoKICAgIGludCBjYWxjdWxhdGUoaW50IGEsIGludCBiLCBpbnQgYywgaW50IG0pewogICAgICAgIG1hcDxpbnQsIGludD4gZmFjdG9ycyA9IGZhY3Rvcml6ZShtKTsKICAgICAgICBpbnQgYW5zID0gMDsKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsIHBpPj4gdjsKICAgICAgICBmb3IoYXV0byBwIDogZmFjdG9ycyl7CiAgICAgICAgICAgIGludCBtb2QgPSAoaW50KXBvdyhwLmZmLCBwLnNzKTsKICAgICAgICAgICAgaWYoYSAlIHAuZmYgIT0gMCl7CiAgICAgICAgICAgICAgICBpbnQgeCA9IGJwb3coYiwgYywgcGhpKG1vZCkpOwogICAgICAgICAgICAgICAgaW50IGNpID0gYnBvdyhhLCB4LCBtb2QpOwogICAgICAgICAgICAgICAgaW50IE4gPSBtIC8gbW9kOwogICAgICAgICAgICAgICAgaW50IGludk4gPSBicG93KE4sIG1vZC0yLCBtb2QpOwogICAgICAgICAgICAgICAgdi5wdXNoX2JhY2soe2NpLCB7TiwgaW52Tn19KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IoYXV0byBwIDogdil7CiAgICAgICAgICAgIGFucyArPSAoaW50KSgoKChwLmZmICogMUxMICogcC5zcy5mZikgJSBtKSAqIDFMTCAqIHAuc3Muc3MpICUgbSk7CiAgICAgICAgICAgIGFucyAlPSBtOwogICAgICAgIH0KICAgICAgICByZXR1cm4gYW5zOwogICAgfQp9OwoKdm9pZCBzb2x2ZShpbnQgdGVzdGNhc2UpewogICAgU2ltcGxlTWF0aFByb2JsZW0gKm9iaiA9IG5ldyBTaW1wbGVNYXRoUHJvYmxlbSgpOwogICAgY291dCA8PCBvYmotPmNhbGN1bGF0ZSgzNzM3MzczLCA3MzczNzM3LCAzNzM3MywgNzM3MzczNzM3KTsKfQoKaW50IG1haW4oKSB7CiAgICAjaWZkZWYgVlBBTAogICAgZnJlb3BlbigiaW4udHh0IiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJvdXQudHh0IiwgInciLCBzdGRvdXQpOwogICAgI2VuZGlmCiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICBjb3V0LnRpZShOVUxMKTsKICAgIGNvdXQgPDwgZml4ZWQgPDwgc2V0cHJlY2lzaW9uKDIwKTsKICAgIGNsb2NrX3QgYiA9IGNsb2NrKCk7CiAgICBpbnQgdCA9IDE7CiAgICAvL2NpbiA+PiB0OwogICAgcmVwKHR0LCAxLCB0KSBzb2x2ZSh0dCk7CiAgICBjbG9ja190IGUgPSBjbG9jaygpOwogICAgY2VyciA8PCAoZG91YmxlKGUgLSBiKSAvIENMT0NLU19QRVJfU0VDKSA8PCAiIHNlYyI7CiAgICByZXR1cm4gMDsKfQo=