n,k,*L=map(int,(input()+' '+input()).split()) a=sum(L) b=0 for i in L:b=(b*k+i)%a print(b)