a, b, c = map(int, input().split()) ans = 0 for i in range(c): ans += pow(i, b, c) * ((a - i) // c + 1) ans %= c print(ans)