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 maxK = 111;
  const int maxN = 2222;
  const int mod = 998244353;
  int fac[maxN], ifac[maxN], inv[maxN];
  int L[maxN], R[maxN], f[2][maxN][maxK], g[2][maxN][maxK];
  void init() {
    inv[1] = 1;
    fac[0] = ifac[0] = 1;
    fac[1] = ifac[1] = 1;
    for (int i = 2; i < maxN; i++) {
      fac[i] = i * 1LL * fac[i - 1] % mod;
      inv[i] = (mod - mod / i) * 1LL * inv[mod % i] % mod;
      ifac[i] = ifac[i - 1] * 1LL * inv[i] % mod;
    }
  }
  inline int npr(int n, int r) {
    if (r < 0 || r > n) return 0;
    return fac[n] * 1LL * ifac[n - r] % mod;
  }
  inline void add(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    init();
    int N, K;
    cin >> N >> K;
    for (int i = 1; i <= N; i++) {
      int x;
      cin >> x;
      L[i] = x - K;
      R[i] = x + K;
      if (L[i] < 0) L[i] = 0;
    }
    L[0] = R[0] = 0;
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    int flg = 0;
    f[flg][0][0] = 1;
    g[flg][0][0] = 1;
    for (int i = 1; i <= N; i++) {
      int nlg = flg ^ 1;
      memset(f[nlg], 0, sizeof(f[nlg]));
      memset(g[nlg], 0, sizeof(g[nlg]));
      for (int d = 0; d <= i; d++) {
        for (int y = L[i]; y <= R[i]; y++) {
          if (L[i - 1] <= y && y <= R[i - 1]) {
            add(f[nlg][d][y - L[i]],
                1LL * (d + y) * f[flg][d][y - L[i - 1]] % mod);
            if (d > 0) {
              add(f[nlg][d][y - L[i]], f[flg][d - 1][y - L[i - 1]]);
            }
          }
          if (y > L[i - 1]) {
            int ny = min(y - 1, R[i - 1]);
            int nd = d + y - 1 - ny;
            add(f[nlg][d][y - L[i]],
                1LL * ifac[d] * g[flg][nd][ny - L[i - 1]] % mod);
          }
        }
      }
      for (int d = i; d >= 0; d--) {
        for (int y = L[i]; y <= R[i]; y++) {
          g[nlg][d][y - L[i]] = fac[d] * 1LL * f[nlg][d][y - L[i]] % mod;
          if (y > L[i]) {
            add(g[nlg][d][y - L[i]], g[nlg][d + 1][y - L[i] - 1]);
          }
        }
      }
      flg = nlg;
    }
    int answer = 0;
    for (int d = 0; d <= N; d++) {
      for (int x = L[N]; x <= R[N]; x++) {
        add(answer, npr(N - x, d) * 1LL * f[flg][d][x - L[N]] % mod);
      }
    }
    cout << answer << '\n';
    return 0;
  }
  