#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
template <typename T>
void r1(T &x) {
  x = 0;
  char c(getchar());
  int f(1);
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') f = -1;
  for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48);
  x *= f;
}
const int mod = 998244353;
template <typename T, typename... Args>
inline void r1(T &t, Args &...args) {
  r1(t);
  r1(args...);
}
const int maxn = 1e5 + 5;
int n, a[maxn], s[maxn], tot(0);
int d[2][maxn];
int inc(int x, int y) { return x += y - mod, x + ((x >> 31) & mod); }
void add(int &x, int y) { x = inc(x, y); }
int Mod(int x) { return x -= mod, x + ((x >> 31) & mod); }
signed main() {
  int i, j;
  r1(n);
  for (i = 1; i <= n; ++i) r1(a[i]);
  for (i = 1; i <= n; i += a[i]) {
    s[++tot] = (a[i] > 1);
    for (j = i; j <= i + a[i] - 1; ++j)
      if (a[i] != a[j]) return puts("0"), 0;
  }
  d[s[1]][0] = 1;
  for (i = 1; i < tot; ++i) {
    if (s[i + 1]) {
      for (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 (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), fac(1);
  for (i = 1; i <= tot; ++i) {
    fac = fac * i % mod;
    long long tmp = (d[0][i - 1] + 2ll * d[1][i - 1]) % mod * fac % mod;
    if ((tot - i) & 1)
      ans = Mod(ans - tmp + mod);
    else
      ans = Mod(ans + tmp);
  }
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Create a solution in cpp for the following problem:

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>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
template <typename T>
void r1(T &x) {
  x = 0;
  char c(getchar());
  int f(1);
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') f = -1;
  for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48);
  x *= f;
}
const int mod = 998244353;
template <typename T, typename... Args>
inline void r1(T &t, Args &...args) {
  r1(t);
  r1(args...);
}
const int maxn = 1e5 + 5;
int n, a[maxn], s[maxn], tot(0);
int d[2][maxn];
int inc(int x, int y) { return x += y - mod, x + ((x >> 31) & mod); }
void add(int &x, int y) { x = inc(x, y); }
int Mod(int x) { return x -= mod, x + ((x >> 31) & mod); }
signed main() {
  int i, j;
  r1(n);
  for (i = 1; i <= n; ++i) r1(a[i]);
  for (i = 1; i <= n; i += a[i]) {
    s[++tot] = (a[i] > 1);
    for (j = i; j <= i + a[i] - 1; ++j)
      if (a[i] != a[j]) return puts("0"), 0;
  }
  d[s[1]][0] = 1;
  for (i = 1; i < tot; ++i) {
    if (s[i + 1]) {
      for (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 (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), fac(1);
  for (i = 1; i <= tot; ++i) {
    fac = fac * i % mod;
    long long tmp = (d[0][i - 1] + 2ll * d[1][i - 1]) % mod * fac % mod;
    if ((tot - i) & 1)
      ans = Mod(ans - tmp + mod);
    else
      ans = Mod(ans + tmp);
  }
  printf("%lld\n", ans);
  return 0;
}

```