A, B, C = map(int,input().split()) print(sum((A - i + C) // C * pow(i, B, C) for i in range(C)) % C)