N, K = map(int, input().split()) A = list(map(int, input().split())) res = 0 num = 1 for a in reversed(A): res = (res + a * num) % sum(A) num = (num * K) % sum(A) print(res)