N,K=map(int,input().split()) A=list(map(int,input().split())) A_sum=sum(A) X=0 power=1 for a in A[::-1]: X+=a*power power*=K power%=A_sum print(X%A_sum)