#include using namespace std; using ll = long long; using ull = unsigned long long; using pairi = pair; #define fi first #define se second const ll INF = 4e18; const ll MOD = 1e9 + 7; const ll base = 31; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll int_pow(ll a, ll base, ll mod = MOD) { ll res = 1; a %= mod; while (base) { if (base & 1) res = res * a % mod; a = a * a % mod; base >>= 1; } return res; } ll inv(ll n) { return int_pow(n, MOD - 2); } void solve() { ll ans = 0; ll h, w, k; cin >> h >> w >> k; vector dh, dw; for (ll i = 1; i * i <= h; i++) { if (h % i == 0) { dh.push_back(i); if (i * i != h) dh.push_back(h / i); } } for (ll i = 1; i * i <= w; i++) { if (w % i == 0) { dw.push_back(i); if (i * i != w) dw.push_back(w / i); } } sort(dh.rbegin(), dh.rend()); sort(dw.rbegin(), dw.rend()); ll nh = dh.size(), nw = dw.size(); vector gh(nh), gw(nw); for (int i = 0; i < nh; i++) { gh[i] = h / dh[i]; for (int j = 0; j < i; j++) { if (dh[j] % dh[i] == 0) gh[i] -= gh[j]; } } for (int i = 0; i < nw; i++) { gw[i] = w / dw[i]; for (int j = 0; j < i; j++) { if (dw[j] % dw[i] == 0) gw[i] -= gw[j]; } } for (int i = 0; i < nh; i++) { for (int j = 0; j < nw; j++) { ll po = dh[i] * dw[j] % (MOD - 1) * __gcd(h / dh[i], w / dw[j]) % (MOD - 1); ll ad = gh[i] * gw[j] % MOD * int_pow(k, po) % MOD; ans = (ans + ad) % MOD; } } ans = ans * inv(h * w) % MOD; cout << ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr << "Program started.\n"; auto start = chrono::steady_clock::now(); string problem = "std"; if (fopen((problem + ".INP").c_str(), "r")) { freopen((problem + ".INP").c_str(), "r", stdin); freopen((problem + ".OUT").c_str(), "w", stdout); } int testcase = 1; //cin >> testcase; while (testcase--) solve(); auto end = chrono::steady_clock::now(); cerr << "Program finished in " << chrono::duration_cast(end - start).count() << " ms."; }