local mfl, mce = math.floor, math.ceil local a, b, c = io.read("*n", "*n", "*n") if c == 1 then print(0) os.exit() end local function modpow(src) local pow = b local res = 1 while 0 < pow do if pow % 2 == 1 then res = (res * src) % c pow = pow - 1 end src = (src * src) % c pow = mfl(pow / 2) end return res end local t = {1} for i = 2, c - 1 do t[i] = (t[i - 1] + modpow(i)) % c end local mul = mfl(a / c) local rem = a % c local ret = (t[c - 1] * mul) % c if 0 < rem then ret = (ret + t[rem]) % c end print(ret)