import math first_num = input() first_s = first_num.split(" ") max_A = int(first_s[0]) rui = int(first_s[1]) wari = int(first_s[2]) tasi = 0 while(max_A>0): tasi = tasi + math.pow(max_A,rui) max_A = max_A-1 tasi = int(tasi%wari) print(tasi)