N,K=map(int,input().split()) A=list(map(int,input().split())) tmp=1 sum_n=0 sum_b=0 for i in A[::-1]: sum_n+=tmp*i sum_b+=i tmp*=K print(sum_n%sum_b)