import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken().to!int; } long readLong() { return readToken().to!long; } real readReal() { return readToken().to!real; } void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } // a^-1 (mod m) long modInv(long a, long m) in { assert(m > 0, "modInv: m > 0 must hold"); } do { long b = m, x = 1, y = 0, t; for (; ; ) { t = a / b; a -= t * b; if (a == 0) { assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1"); if (b == -1) { y = -y; } return (y < 0) ? (y + m) : y; } x -= t * y; t = b / a; b -= t * a; if (b == 0) { assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1"); if (a == -1) { x = -x; } return (x < 0) ? (x + m) : x; } y -= t * x; } } immutable MO = 10L^^9 + 7; long power(long a, long e) { long x = a % MO, y = 1; for (; e; e >>= 1) { if (e & 1) { (y *= x) %= MO; } (x *= x) %= MO; } return y; } Tuple!(long, long)[] getDivisors(long n) { Tuple!(long, long)[] ds; for (long d = 1; d * d <= n; ++d) { if (n % d == 0) { ds ~= tuple(d, 0L); if (d != n / d) { ds ~= tuple(n / d, 0L); } } } ds.sort; foreach_reverse (i; 0 .. ds.length) { ds[i][1] = n / ds[i][0]; foreach_reverse (j; i + 1 .. ds.length) { if (ds[j][0] % ds[i][0] == 0) { ds[i][1] -= ds[j][1]; } } } return ds; } long M, N, K; void main() { try { for (; ; ) { M = readLong(); N = readLong(); K = readLong(); const dsM = getDivisors(M); const dsN = getDivisors(N); debug { writeln("M: ", dsM); writeln("N: ", dsN); } long ans; foreach (x; dsM) foreach (y; dsN) { /* (M, 0), (0, N), (a, b) det = gcd(M N, M b, a N) */ long prod = power(K, gcd(M * y[0], x[0] * N)); (prod *= x[1]) %= MO; (prod *= y[1]) %= MO; (ans += prod) %= MO; } (ans *= modInv(M * N, MO)) %= MO; writeln(ans); } } catch (EOFException e) { } }