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>
  #pragma GCC optimize("O3,unroll-loops")
  #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  using namespace std;
  using ll = long long;
  using pii = pair<int, int>;
  void _debug() { std::cerr << std::endl; }
  template <typename T, typename... U>
  void _debug(T t, U... args) {
    std::cerr << " " << t;
    _debug(args...);
  }
  const int maxn = 2200;
  const int mod = 998244353;
  void add(int &x, int v) {
    x += v;
    if (x >= mod) x -= mod;
  }
  int qpow(int a, int b) {
    int ans = 1;
    while (b) {
      if (b & 1) ans = 1ll * a * ans % mod;
      b >>= 1;
      a = 1ll * a * a % mod;
    }
    return ans;
  }
  int fac[maxn], inv[maxn];
  int C(int i, int j) {
    if (i < j) return 0;
    return 1ll * fac[i] * inv[j] % mod * inv[i - j] % mod;
  }
  int invC(int i, int j) {
    if (i < j) return 0;
    return 1ll * fac[j] * fac[i - j] % mod * inv[i] % mod;
  }
  void init() {
    fac[0] = 1;
    for (int i = 1; i < maxn; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
    inv[maxn - 1] = qpow(fac[maxn - 1], mod - 2);
    for (int i = maxn - 2; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  }
  int Inv(int x) { return qpow(x, mod - 2); }
  int f[2][maxn][maxn], n, k, pf[maxn][maxn], b[maxn], mx[maxn], invc[maxn][maxn],
      c[maxn][maxn];
  int main() {
    init();
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= i; j++) invc[i][j] = invC(i, j), c[i][j] = C(i, j);
    }
    for (int i = 1; i <= n; i++) cin >> b[i];
    vector<pii> vis;
    f[0][0][0] = 1;
    pf[0][0] = 1;
    vis.push_back({0, 0});
    for (int i = 1; i <= n; i++) {
      int now = i & 1, pre = now ^ 1;
      for (int j = max(0, b[i] - k); j <= min(b[i] + k, n + 1); j++) {
        for (int g = 0; g <= i; g++) {
          add(f[now][j][g], 1ll * (j + g) * f[pre][j][g] % mod);
          add(f[now][j][g + 1], 1ll * (n - j - g) * f[pre][j][g] % mod);
        }
      }
      for (int j = max(0, b[i] - k); j <= min(b[i] + k, n + 1); j++) {
        for (int g = 0; g <= i; g++) {
          if (j)
            add(f[now][j][g], 1ll * pf[j + g - 1][min(mx[j + g - 1], j - 1)] *
                                  c[n - j][g] % mod);
        }
      }
      memset(mx, 0, sizeof(mx));
      for (const auto &[u, v] : vis) pf[u][v] = 0, f[pre][v][u - v] = 0;
      vis.clear();
      vis.reserve(2 * k * i);
      for (int j = max(0, b[i] - k); j <= min(b[i] + k, n + 1); j++) {
        for (int g = 0; g <= i; g++) {
          assert(j >= 0);
          add(pf[j + g][j], 1ll * f[now][j][g] * invc[n - j][g] % mod);
          vis.push_back({j + g, j});
          mx[j + g] = max(mx[j + g], j);
        }
      }
      for (const auto &[u, v] : vis)
        if (v) add(pf[u][v], pf[u][v - 1]);
    }
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) add(ans, f[n & 1][i][j]);
    }
    cout << ans;
  }
  