# O(N*M) n,m=map(int,input().split()) a=list(map(int,input().split())) odd=0 even=0 for i in range(n): if i%2==0: odd+=a[i] else: even+=a[i] if (odd-even)%m!=0: print(-1) exit() if n*m>=5*10**7: raise Exception mod=998244353 M=m-1 powM=[1] for i in range(n+10): powM.append(powM[-1]*M%mod) invm=pow(m,mod-2,mod) ans=0 res=0 for i in range(n): ai=a[i] k=n-1-i x=powM[k] if k%2==0: y=(x-1)*invm y%=mod else: y=(x+1)*invm y%=mod for j in range(ai): if k%2==0: if (res+j)%m==0: ans+=y+1 else: ans+=y else: if (res-j+m-1)%m==0: ans+=y-1 else: ans+=y if i%2==0: res+=ai else: res-=ai ans%=mod #print(x,y) ans+=1 print(ans%mod)