def solve(): N,M=map(int,input().split()) A=list(map(int,input().split())) # いい数列かの確認 S=0 for i in range(N): S+=(1 if i%2==0 else -1)*A[i] if S%M: return -1 Mod=998244353 X=0 for a in A: X=((M-1)*X+a)%Mod return (X*pow(M,Mod-2,Mod)+1)%Mod #================================================== print(solve())