package no2xxx; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class No2847 { static Scanner in; static PrintWriter out; static String INPUT = ""; static void solve() { sum = 0; X = ni(); Y = ni(); mod = ni(); runSternBrocot(); for(long r = 1;2*r <= X;r++){ sum += 2L * (X-2*r) * Y % mod; } for(long r = 1;2*r <= Y;r++){ sum += 2L * (Y-2*r) * X % mod; } sum %= mod; out.println(sum); } static long X, Y; static long sum; static long mod; static void go(long n, long d) { // tr(n, d); long A = d*d-n*n; long B = 2*d*n; long C = d*d+n*n; long lsum = 0; { long m = Math.min(X / 2 / A, Y / 2 / B); // sum_i (X-2Ai)(Y-2Bi) long sm = m*(m+1)/2%mod; long ssm = m*(m+1)*(2*m+1)/6%mod; lsum += X*Y%mod*m%mod; lsum += -2L*A*Y%mod*sm%mod; lsum += -2L*B*X%mod*sm%mod; lsum += 4L*A*B%mod*ssm%mod; } { long m = Math.min(Y / 2 / A, X / 2 / B); // sum_i (Y-2Ai)(X-2Bi) long sm = m*(m+1)/2%mod; long ssm = m*(m+1)*(2*m+1)/6%mod; lsum += X*Y%mod*m%mod; lsum += -2L*A*X%mod*sm%mod; lsum += -2L*B*Y%mod*sm%mod; lsum += 4L*A*B%mod*ssm%mod; } // if(lsum > 0){ // tr(X, Y, A, B); // } sum += lsum * 4; sum %= mod; if(sum < 0)sum += mod; } public static long invl(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } public static void runSternBrocot() { long sn = 0, sd = 1, gn = 1, gd = 1; while(true){ long mn = sn+gn, md = sd+gd; if((md*md-mn*mn) <= 500000L && 2L*md*mn <= 500000L){ // 親が満たさないなら子がどちらも満たさなくなる条件 // 条件を満たしているなら左におりる if(((mn^md)&1) == 1)go(mn, md); gn = mn; gd = md; }else{ // 条件を満たさないなら右からあがれるだけあがり、最後に左に1個あがったあと右に降りる while(sd > gd){ sn -= gn; sd -= gd; } gn -= sn; gd -= sd; if(gd == 0)break; sn += gn; sd += gd; } } } public static void main(String[] args) throws Exception { in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT); out = new PrintWriter(System.out); solve(); out.flush(); } static int ni() { return Integer.parseInt(in.next()); } static long nl() { return Long.parseLong(in.next()); } static double nd() { return Double.parseDouble(in.next()); } static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }