#include const int Mod = 998244353; long long div_mod(long long x, long long y, long long z) { if (x % y == 0) return x / y; else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y; } long long pow_mod(int n, long long k) { long long N, ans = 1; for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod; return ans; } int main() { int i, N, M, A[200001]; scanf("%d %d", &N, &M); for (i = 1; i <= N; i++) scanf("%d", &(A[i])); long long B[200001]; for (i = 1; i <= N; i++) B[i] = A[i]; for (i = 1; i < N; i++) { if (B[i] % M == 0) continue; B[i+1] += M - B[i] % M; B[i] += M - B[i] % M; } if (B[N] % M != 0) { printf("-1\n"); return 0; } int excep[2] = {0, M - 1}; long long ans = 1, inv = div_mod(1, M, Mod); for (i = 1; i < N; i++) { if ((N - i) % 2 == 0) { ans += ((pow_mod(M - 1, N - i - 1) + 1) * (M - 1) % Mod * inv % Mod - 1) * A[i] % Mod; if (excep[1] - (A[i] - 1) < 0 || (excep[1] == M - 1 && A[i] > 0)) ans++; excep[0] -= A[i]; excep[1] += A[i]; } else { ans += ((pow_mod(M - 1, N - i - 1) - 1) * (M - 1) % Mod * inv % Mod + 1) * A[i] % Mod; if (excep[0] + A[i] - 1 >= M - 1) ans--; excep[0] -= A[i]; excep[1] += A[i]; } if (excep[0] >= M) excep[0] -= M; else if (excep[0] < 0) excep[0] += M; if (excep[1] >= M) excep[1] -= M; else if (excep[1] < 0) excep[1] += M; } printf("%lld\n", ans % Mod); fflush(stdout); return 0; }