n,k=map(int,input().split()) a=list(map(int,input().split())) S=sum(a) now=0 for i in a: now=(now*k+i)%S print(now)