description: For an array c of nonnegative integers, MEX(c) denotes the smallest nonnegative integer that doesn't appear in it. For example, MEX([0, 1, 3]) = 2, MEX([42]) = 0.
  
  You are given integers n, k, and an array [b_1, b_2, …, b_n].
  
  Find the number of arrays [a_1, a_2, …, a_n], for which the following conditions hold:
  
    * 0 ≤ a_i ≤ n for each i for each i from 1 to n.
  
    * |MEX([a_1, a_2, …, a_i]) - b_i| ≤ k for each i from 1 to n.
  
  
  
  
  As this number can be very big, output it modulo 998 244 353.
  
  Input
  
  The first line of the input contains two integers n, k (1 ≤ n ≤ 2000, 0 ≤ k ≤ 50).
  
  The second line of the input contains n integers b_1, b_2, …, b_n (-k ≤ b_i ≤ n+k) — elements of the array b.
  
  Output
  
  Output a single integer — the number of arrays which satisfy the conditions from the statement, modulo 998 244 353.
  
  Examples
  
  Input
  
  
  4 0
  0 0 0 0
  
  
  Output
  
  
  256
  
  
  Input
  
  
  4 1
  0 0 0 0
  
  
  Output
  
  
  431
  
  
  Input
  
  
  4 1
  0 0 1 1
  
  
  Output
  
  
  509
  
  
  Input
  
  
  5 2
  0 0 2 2 0
  
  
  Output
  
  
  6546
  
  
  Input
  
  
  3 2
  -2 0 4
  
  
  Output
  
  
  11
solution: #include <bits/stdc++.h>
  using namespace std;
  const int P = 998244353;
  int dp[103][2003], tp[103][2003], R[2003], TR[2003], N, K, A[2003];
  int upd(int x) { return x + (x >> 31 & P); }
  int add(int x, int y) { return upd(x + y - P); }
  int sub(int x, int y) { return upd(x - y); }
  int main() {
    cin >> N >> K;
    for (int i = 1; i <= N; ++i) cin >> A[i];
    for (int i = A[N] - K; i <= A[N] + K; ++i)
      if (i >= 0 && i <= N) dp[i + K - A[N]][1] = 1;
    A[0] = -K;
    for (int i = N; i; --i) {
      memset(tp, 0, sizeof(tp));
      for (int j = 0; j <= 2 * K; ++j)
        if (A[i] + j - K >= A[i - 1] - K && A[i] + j - K <= A[i - 1] + K) {
          int pos = A[i] + j - K - A[i - 1] + K;
          for (int l = 0; l <= N + 1; ++l)
            if (dp[j][l])
              tp[pos][l] = (tp[pos][l] + 1ll * dp[j][l] * (N + 1 - l)) % P;
        }
      memset(R, 0, sizeof(R));
      for (int t = A[i] + K - 1; t >= A[i - 1] - K; --t) {
        for (int j = N + 1; ~j; --j)
          if (R[j]) R[j + 1] = sub(R[j + 1], R[j]);
        if (t + 1 >= A[i] - K)
          for (int j = 0; j <= N + 1; ++j)
            R[j] = add(R[j], dp[t + 1 - A[i] + K][j]);
        if (t <= A[i - 1] + K)
          for (int j = 0; j <= N + 1; ++j)
            tp[t - A[i - 1] + K][j + 1] = add(tp[t - A[i - 1] + K][j + 1], R[j]);
      }
      memcpy(dp, tp, sizeof(dp));
    }
    int sum = 0;
    for (int i = 0; i <= N + 1; ++i) sum = (sum + dp[2 * K][i]) % P;
    cout << sum;
    return 0;
  }
  