description: For a permutation p of numbers 1 through n, we define a stair array a as follows: a_i is length of the longest segment of permutation which contains position i and is made of consecutive values in sorted order: [x, x+1, …, y-1, y] or [y, y-1, …, x+1, x] for some x ≤ y. For example, for permutation p = [4, 1, 2, 3, 7, 6, 5] we have a = [1, 3, 3, 3, 3, 3, 3]. 
  
  You are given the stair array a. Your task is to calculate the number of permutations which have stair array equal to a. Since the number can be big, compute it modulo 998 244 353. Note that this number can be equal to zero.
  
  Input
  
  The first line of input contains integer n (1 ≤ n ≤ 10^5) — the length of a stair array a.
  
  The second line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
  
  Output
  
  Print the number of permutations which have stair array equal to a. Since the number can be big, compute it modulo 998 244 353.
  
  Examples
  
  Input
  
  
  6
  3 3 3 1 1 1
  
  
  Output
  
  
  6
  
  
  Input
  
  
  7
  4 4 4 4 3 3 3
  
  
  Output
  
  
  6
  
  
  Input
  
  
  1
  1
  
  
  Output
  
  
  1
  
  
  Input
  
  
  8
  2 2 2 2 2 2 1 1
  
  
  Output
  
  
  370
  
  
  Input
  
  
  4
  3 2 3 1
  
  
  Output
  
  
  0
solution: #include <bits/stdc++.h>
  using namespace std;
  template <typename T>
  bool chkmax(T &x, T y) {
    return x < y ? x = y, true : false;
  }
  template <typename T>
  bool chkmin(T &x, T y) {
    return x > y ? x = y, true : false;
  }
  int readint() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
      if (ch == '-') f = -1;
      ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
      x = x * 10 + ch - '0';
      ch = getchar();
    }
    return x * f;
  }
  const int cys = 998244353;
  int n, m;
  int a[100005], b[100005], d[2][100005];
  int mod(int x) { return x >= cys ? x - cys : x; }
  void add(int &x, int y) { x = mod(x + y); }
  int main() {
    n = readint();
    for (int i = 1; i <= n; i++) a[i] = readint();
    bool fl = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= i + a[i] - 1; j++)
        if (a[j] != a[i]) fl = 1;
      i += a[i] - 1;
      b[++m] = a[i] > 1;
    }
    if (fl) return printf("0\n"), 0;
    d[b[1]][0] = 1;
    for (int i = 1; i < m; i++) {
      if (b[i + 1]) {
        for (int j = i - 1; j >= 0; j--) {
          add(d[1][j + 1], mod(d[1][j] << 1));
          add(d[1][j + 1], d[0][j]);
          add(d[1][j], d[0][j]);
          d[0][j] = 0;
        }
      } else {
        for (int j = i - 1; j >= 0; j--) {
          add(d[0][j + 1], mod(d[1][j] << 1));
          add(d[0][j + 1], d[0][j]);
          add(d[1][j], d[0][j]);
          d[0][j] = 0;
        }
      }
    }
    long long ans = 0, now = 1;
    for (int i = 1; i <= m; i++) {
      now = now * i % cys;
      long long tmp = (d[0][i - 1] + 2ll * d[1][i - 1]) % cys * now % cys;
      if ((m - i) & 1)
        ans = mod(ans + cys - tmp);
      else
        ans = mod(ans + tmp);
    }
    printf("%lld\n", ans);
    return 0;
  }
  