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 n, m, a[2005], f[2][2005][105], g[2][2005][105];
  int Bg, Ed, bg, ed, C[2005][2005], ans;
  inline int val(int i, int x) { return a[i] - m + x; }
  inline int id(int i, int x) { return x - a[i] + m; }
  int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    f[0][0][m] = 1;
    for (int j = m; j <= m + m; ++j) g[0][0][j] = 1;
    for (int i = 1; i <= n; ++i) {
      memset(f[i & 1], 0, sizeof(f[0]));
      memset(g[i & 1], 0, sizeof(g[0]));
      for (int j = 1; j <= i; ++j) {
        Bg = max(id(i, 0), 0), Ed = min(id(i, j), m + m);
        for (int x = Bg, k, t, cur; x <= Ed; ++x) {
          k = val(i, x), t = id(i - 1, k), cur = 0;
          if (0 <= t && t <= m + m)
            cur = ((long long)f[~i & 1][j][t] * j +
                   (long long)f[~i & 1][j - 1][t] * (j - k)) %
                  p;
          bg = max(id(i - 1, 0), 0), ed = min(id(i - 1, k - 1), m + m);
          if (bg <= ed)
            cur = ((long long)cur + g[~i & 1][j - 1][ed] -
                   (bg ? g[~i & 1][j - 1][bg - 1] : 0)) %
                  p;
          f[i & 1][j][x] = cur;
          if (x > Bg)
            g[i & 1][j][x] = (g[i & 1][j][x - 1] + cur) % p;
          else
            g[i & 1][j][x] = cur;
        }
      }
    }
    for (int i = 0; i <= n; ++i) {
      for (int j = 1; j <= i; ++j) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % p;
      C[i][0] = 1;
    }
    for (int j = 1; j <= n; ++j) {
      Bg = max(id(n, 0), 0), Ed = min(id(n, j), m + m);
      for (int x = Bg, k; x <= Ed; ++x) {
        k = val(n, x);
        ans = (ans + (long long)f[n & 1][j][x] * C[n - k][j - k]) % p;
      }
    }
    printf("%d\n", (ans + p) % p);
    return 0;
  }
  