n,k=map(int,input().split()) L=list(map(int,input().split())) a=sum(L) b=0 for i in L: n-=1 if i>0:b+=pow(k,n)*i print(b%a)