A, B, C = map(int, input().split()) ans = 0 for i in range(1, min(A+1, C)): ans += pow(i, B, C) ans %= C print(ans)