n,k=map(int,input().split()) a=list(map(int,input().split())) a=a[::-1] mod=sum(a) num=0 for i in range(n): num+=a[i]*pow(k,i,mod) print(num%mod)