import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); int c = sc.nextInt(); long div = a / c % c; long mod = a % c; long ans = 0; for (int i = 1; i < c; i++) { if (i <= mod) { ans += powMod(i, b, c) * (div + 1) % c; } else { ans += powMod(i, b, c) * div % c; } ans %= c; } System.out.println(ans); } static long powMod(long x, long y, long mod) { if (y == 0) { return 1; } else if (y % 2 == 0) { return powMod(x * x % mod, y / 2, mod); } else { return powMod(x, y - 1, mod) * x % mod; } } }