def powf(x,n): if n == 0: return 1 k = 1 while n>1: if n%2 != 0: k *= x x *= x n //= 2 return k*x n,k = map(int,input().split()) a = list(map(int,input().split())) s = sum(a) ans = 0 cnt = 1 for i in range(n): ans += a[n-1-i]*cnt cnt *= k print(ans%s)