mod = 998244353 def isgood(n,m,a): g = 0 for i in range(n): if i % 2 == 0: g += a[i] else: g -= a[i] if g % m == 0: return True return False def count(n,m,e,l,t): if n % 2 == 0: r = (pow(m-1, n, mod) - 1) * pow(m, mod-2, mod) % mod if e-l < 0 <= e: return (l*r+1) % mod return l*r % mod else: r = (pow(m-1, n, mod) + 1) * pow(m, mod-2, mod) % mod if t == 1: if e-l < 1 <= e or e-l < 1-m <= e: return (l*r-1) % mod return l*r % mod else: if e-l < m-1 <= e or e-l < -1 <= e: return (l*r-1) % mod return l*r % mod n,m = map(int,input().split()) a = list(map(int,input().split())) if isgood(n,m,a): g = 0 ans = 0 for i in range(n): ans += count(n-i-1,m,g,a[i],i%2) g = (g+a[i]) % m #print(ans) print((ans+1) % mod) else: print(-1)