print((function (f, fh, m) local x,n,r = fh:read("*n"),fh:read("*n"),0 for _=1,n do r=r+f(x,fh:read("*n"),m)%m end return r%m end)(function (x, n, m) local ba,rs,r = bit.band,bit.rshift,1 while n~=0 do r,x,n=ba(n,1)~=0 and r*(x%m) or r,x*(x%m),rs(n,1) end return r%m end, io.stdin, 1000003))