A, B, C = map(int, input().split()) res = 0 for i in range(1, min(A, C) + 1): res += pow(i, B, C) res %= C if A < C: print(res) exit() res *= A // C res %= C A %= C for i in range(1, A): res += pow(i, B, C) res %= C print(res)