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