import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.io.PrintWriter import java.util.* fun PrintWriter.solve(sc: FastScanner) { val a = sc.nextLong() val b = sc.nextLong() val c = sc.nextInt() val arr = Array(c) { 0L } for (i in 1 until c) { arr[i] = modpow(i.toLong(), b, c) } val sum = Array(c + 1) { 0L } for (i in 0 until c) { sum[i + 1] = sum[i] + arr[i] } var ans = sum[c] * (a / c) % c + sum[((a + 1) % c).toInt()] ans %= c println(ans) } fun modpow(a:Long, b:Long, c:Int): Long { if (b == 0L) return if (a % c == 0L) 0 else 1 if (b == 1L) return a % c val p = modpow(a, b / 2, c) val pp = p * p % c return if (b % 2 == 0L) pp else pp * a % c } fun main() { val writer = PrintWriter(System.out, false) writer.solve(FastScanner(System.`in`)) writer.flush() } class FastScanner(s: InputStream) { private var st = StringTokenizer("") private val br = BufferedReader(InputStreamReader(s)) fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() fun ready() = br.ready() }