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;
  template <typename T>
  void read(T &t) {
    t = 0;
    char ch = getchar();
    int f = 1;
    while (ch < '0' || ch > '9') {
      if (ch == '-') f = -1;
      ch = getchar();
    }
    do {
      (t *= 10) += ch - '0';
      ch = getchar();
    } while ('0' <= ch && ch <= '9');
    t *= f;
  }
  template <typename T>
  void write(T t) {
    if (t < 0) {
      putchar('-');
      write(-t);
      return;
    }
    if (t > 9) write(t / 10);
    putchar('0' + t % 10);
  }
  template <typename T>
  void writeln(T t) {
    write(t);
    puts("");
  }
  const int mod = 998244353;
  const int maxn = 2010;
  int n, k, jc[maxn], ivjc[maxn], iv[maxn];
  int dp[2][maxn][maxn], L[maxn], R[maxn], ans, tmp[maxn][maxn * 2];
  void update(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
  }
  int C(int x, int y) {
    if (x < y || y < 0) return 0;
    return (long long)jc[x] * ivjc[y] % mod * ivjc[x - y] % mod;
  }
  int main() {
    read(n), read(k);
    iv[0] = jc[0] = ivjc[0] = iv[1] = jc[1] = ivjc[1] = 1;
    for (int i = 2; i < maxn; i++) {
      jc[i] = (long long)jc[i - 1] * i % mod;
      iv[i] = (long long)iv[mod % i] * (mod - mod / i) % mod;
      ivjc[i] = (long long)ivjc[i - 1] * iv[i] % mod;
    }
    for (int i = 1; i <= n; i++) {
      int x;
      read(x);
      L[i] = max(0, x - k), R[i] = min(n + 1, x + k);
    }
    dp[0][0][0] = 1;
    for (int i = 1; i <= n + 1; i++) {
      for (int mex = L[i]; mex <= R[i]; ++mex)
        for (int cnt = 0; cnt <= i; ++cnt) dp[i & 1][mex][cnt] = 0;
      for (int mex = L[i - 1]; mex <= R[i - 1]; ++mex) {
        if (i <= n) {
          memset(tmp[mex], 0, sizeof(tmp[mex]));
          if (mex > L[i - 1]) memcpy(tmp[mex], tmp[mex - 1], sizeof(tmp[mex]));
        }
        for (int cnt = 0; cnt <= i; cnt++)
          if (dp[i & 1 ^ 1][mex][cnt]) {
            if (i > n) {
              update(ans, (long long)dp[i & 1 ^ 1][mex][cnt] * C(n - mex, cnt) %
                              mod * jc[cnt] % mod);
              continue;
            }
            update(dp[i & 1][mex][cnt],
                   (long long)dp[i & 1 ^ 1][mex][cnt] * (mex + cnt) % mod);
            update(dp[i & 1][mex][cnt + 1], dp[i & 1 ^ 1][mex][cnt]);
            update(tmp[mex][mex + cnt],
                   (long long)dp[i & 1 ^ 1][mex][cnt] * jc[cnt] % mod);
          }
      }
      if (i > n) break;
      for (int m = L[i]; m <= R[i]; m++)
        for (int B = 0; B <= i; B++) {
          int x = m - 1;
          if (x > R[i - 1]) x = R[i - 1];
          if (x < L[i - 1]) continue;
          int y = B + m - 1;
          if (y < 0 || y > n * 2) continue;
          update(dp[i & 1][m][B], (long long)tmp[x][y] * ivjc[B] % mod);
        }
    }
    printf("%d\n", ans);
    return 0;
  }
  