f=lambda:map(int,input().split()) n,k=f() *a,=f() print(sum([v*k**(len(a)-i-1) for i,v in enumerate(a)])%sum([v for v in a]))