import sys readline=sys.stdin.readline write=sys.stdout.write N,K=map(int,readline().split()) A=list(map(int,readline().split()))[::-1] S=sum(A) ans=0 for n in range(N): ans+=A[n]*pow(K,n,S)%S ans%=S print(ans)