#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &first, T second) {
  first < second ? first = second : T();
}
template <typename T>
void chkmin(T &first, T second) {
  second < first ? first = second : T();
}
template <typename T>
void readint(T &first) {
  first = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) first = first * 10 + (c - '0');
  first *= f;
}
const int MOD = 998244353;
inline int dmy(int first) { return first >= MOD ? first - MOD : first; }
inline void inc(int &first, int second) {
  (first += second) >= MOD ? first -= MOD : 0;
}
int qmi(int first, int second) {
  int ans = 1;
  for (; second; second >>= 1, first = 1ll * first * first % MOD)
    if (second & 1) ans = 1ll * ans * first % MOD;
  return ans;
}
const int MAXN = 100005;
int n, a[MAXN], dp[2][MAXN];
int m, b[MAXN], fac[MAXN];
int main() {
  readint(m);
  for (int i = 1; i <= m; ++i) readint(b[i]);
  for (int u = 1; u <= m; u += b[u]) {
    if (u + b[u] - 1 > m) return 0 * printf("0\n");
    for (int i = u; i <= u + b[u] - 1; ++i)
      if (b[i] != b[u]) return 0 * printf("0\n");
    a[++n] = (b[u] > 1);
  }
  dp[a[1]][1] = 1 + a[1];
  for (int i = 2; i <= n; ++i)
    if (!a[i]) {
      for (int j = i - 1; j; --j)
        inc(dp[0][j + 1], dp[0][j]), inc(dp[0][j + 1], dp[1][j]),
            inc(dp[1][j], dmy(dp[0][j] << 1)), dp[0][j] = 0;
    } else {
      for (int j = i - 1, u; j; --j)
        u = dmy(dp[0][j] << 1), inc(dp[1][j + 1], u),
        inc(dp[1][j + 1], dmy(dp[1][j] << 1)), inc(dp[1][j], u), dp[0][j] = 0;
    }
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD;
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    int t = 1ll * fac[i] * (dp[0][i] + dp[1][i]) % MOD;
    if ((n - i) & 1)
      inc(res, MOD - t);
    else
      inc(res, t);
  }
  printf("%d\n", res);
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &first, T second) {
  first < second ? first = second : T();
}
template <typename T>
void chkmin(T &first, T second) {
  second < first ? first = second : T();
}
template <typename T>
void readint(T &first) {
  first = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) first = first * 10 + (c - '0');
  first *= f;
}
const int MOD = 998244353;
inline int dmy(int first) { return first >= MOD ? first - MOD : first; }
inline void inc(int &first, int second) {
  (first += second) >= MOD ? first -= MOD : 0;
}
int qmi(int first, int second) {
  int ans = 1;
  for (; second; second >>= 1, first = 1ll * first * first % MOD)
    if (second & 1) ans = 1ll * ans * first % MOD;
  return ans;
}
const int MAXN = 100005;
int n, a[MAXN], dp[2][MAXN];
int m, b[MAXN], fac[MAXN];
int main() {
  readint(m);
  for (int i = 1; i <= m; ++i) readint(b[i]);
  for (int u = 1; u <= m; u += b[u]) {
    if (u + b[u] - 1 > m) return 0 * printf("0\n");
    for (int i = u; i <= u + b[u] - 1; ++i)
      if (b[i] != b[u]) return 0 * printf("0\n");
    a[++n] = (b[u] > 1);
  }
  dp[a[1]][1] = 1 + a[1];
  for (int i = 2; i <= n; ++i)
    if (!a[i]) {
      for (int j = i - 1; j; --j)
        inc(dp[0][j + 1], dp[0][j]), inc(dp[0][j + 1], dp[1][j]),
            inc(dp[1][j], dmy(dp[0][j] << 1)), dp[0][j] = 0;
    } else {
      for (int j = i - 1, u; j; --j)
        u = dmy(dp[0][j] << 1), inc(dp[1][j + 1], u),
        inc(dp[1][j + 1], dmy(dp[1][j] << 1)), inc(dp[1][j], u), dp[0][j] = 0;
    }
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD;
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    int t = 1ll * fac[i] * (dp[0][i] + dp[1][i]) % MOD;
    if ((n - i) & 1)
      inc(res, MOD - t);
    else
      inc(res, t);
  }
  printf("%d\n", res);
  return 0;
}

```