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 maxn = 2010, P = 998244353;
  int n, m, ans;
  int fac[maxn], inv[maxn], a[maxn], l[maxn], r[maxn], f[2][maxn][maxn];
  int C(int n, int m) {
    if (n < m) return 0;
    return (long long)fac[n] * inv[m] % P * inv[n - m] % P;
  }
  int main() {
    fac[0] = inv[0] = inv[1] = 1;
    for (int i = 1; i < maxn; i++) fac[i] = (long long)fac[i - 1] * i % P;
    for (int i = 2; i < maxn; i++)
      inv[i] = (long long)(P - P / i) * inv[P % i] % P;
    for (int i = 1; i < maxn; i++) inv[i] = (long long)inv[i - 1] * inv[i] % P;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      l[i] = max(a[i] - m, 0), r[i] = min(a[i] + m, i);
    }
    f[0][0][0] = 1;
    for (int i = 1; i <= n; i++) {
      int p = (i & 1);
      for (int k = 0; k <= i - l[i]; k++) {
        for (int j = max(l[i] - r[i - 1] - 1, 0); j < l[i] - l[i - 1]; j++) {
          f[p][l[i]][k] = (f[p][l[i]][k] + f[p ^ 1][l[i] - j - 1][k + j]) % P;
        }
      }
      for (int j = l[i] + 1; j <= r[i]; j++) {
        for (int k = 0; k <= i - j; k++) {
          f[p][j][k] = (f[p][j - 1][k + 1] + f[p ^ 1][j - 1][k]) % P;
        }
      }
      for (int j = l[i - 1]; j <= r[i - 1]; j++) {
        for (int k = 0; k < i - j; k++) {
          if ((l[i] <= j) && (j <= r[i])) {
            f[p][j][k] = (f[p][j][k] + (long long)(j + k) * f[p ^ 1][j][k]) % P;
            f[p][j][k + 1] =
                (f[p][j][k + 1] + (long long)(k + 1) * f[p ^ 1][j][k]) % P;
          }
        }
      }
      for (int j = l[i - 1]; j <= r[i - 1]; j++) {
        for (int k = 0; k < i - j; k++) {
          f[p ^ 1][j][k] = 0;
        }
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n - i; j++) {
        ans = (ans + (long long)C(n - i, j) * f[n & 1][i][j]) % P;
      }
    }
    printf("%d\n", ans);
    return 0;
  }
  