#include <bits/stdc++.h>
using namespace std;
int ksm(int a, int b) {
  int jg = 1;
  while (b > 0) {
    if (b & 1) jg = 1ll * jg * a % 998244353;
    a = 1ll * a * a % 998244353;
    b >>= 1;
  }
  return jg;
}
int w0[2097152 << 1], w1[2097152 << 1];
void getww(int n) {
  for (int h = 2; h <= n; h <<= 1) {
    int wn = ksm(3, (998244353 - 1) / h);
    for (int i = 0, w = 1; i < (h >> 1); i++, w = 1ll * w * wn % 998244353)
      w0[i + (h >> 1)] = w;
    wn = ksm(332748118, (998244353 - 1) / h);
    for (int i = 0, w = 1; i < (h >> 1); i++, w = 1ll * w * wn % 998244353)
      w1[i + (h >> 1)] = w;
  }
}
void NTT(int sz[2097152], int n, int lx) {
  for (int i = 0, t = 0; i < n; i++) {
    if (i < t) {
      int o = sz[i];
      sz[i] = sz[t];
      sz[t] = o;
    }
    for (int j = (n >> 1); (t ^= j) < j; j >>= 1)
      ;
  }
  for (int h = 2; h <= n; h <<= 1) {
    for (int i = 0; i < n; i += h) {
      for (int j = 0; j < (h >> 1); j++) {
        int w = (lx == 0 ? w0[j + (h >> 1)] : w1[j + (h >> 1)]);
        int z = 1ll * w * sz[i + j + (h >> 1)] % 998244353;
        sz[i + j + (h >> 1)] = (sz[i + j] - z + 998244353) % 998244353;
        sz[i + j] = (sz[i + j] + z) % 998244353;
      }
    }
  }
  if (lx == 1) {
    int nn = ksm(n, 998244353 - 2);
    for (int i = 0; i < n; i++) sz[i] = 1ll * sz[i] * nn % 998244353;
  }
}
int F[2097152], G[2097152];
vector<int> mul(const vector<int>& a, const vector<int>& b) {
  vector<int> rt;
  int le = 1, na = a.size(), nb = b.size();
  while (le < na + nb) le <<= 1;
  for (int i = 0; i < le; i++) F[i] = G[i] = 0;
  for (int i = 0; i < na; i++) F[i] = a[i];
  for (int i = 0; i < nb; i++) G[i] = b[i];
  NTT(F, le, 0);
  NTT(G, le, 0);
  for (int i = 0; i < le; i++) F[i] = 1ll * F[i] * G[i] % 998244353;
  NTT(F, le, 1);
  for (int i = 0; i < na + nb - 1; i++) rt.push_back(F[i]);
  return rt;
}
vector<int> add(const vector<int>& a, const vector<int>& b) {
  vector<int> rt;
  int na = a.size(), nb = b.size();
  rt.resize(na > nb ? na : nb);
  for (int i = 0; i < na; i++) rt[i] = a[i];
  for (int i = 0; i < nb; i++) rt[i] = (rt[i] + b[i]) % 998244353;
  return rt;
}
struct SJd {
  vector<int> sz[2][2];
};
SJd operator*(const SJd& a, const SJd& b) {
  SJd rt;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++)
        rt.sz[i][j] = add(rt.sz[i][j], mul(a.sz[i][k], b.sz[k][j]));
    }
  }
  return rt;
}
SJd zt[100010];
SJd fzntt(int l, int r) {
  if (l == r) return zt[l];
  int m = (l + r) >> 1;
  return fzntt(l, m) * fzntt(m + 1, r);
}
int sz[100010], ss[100010], jc[100010], mi[100010];
SJd caljz(int z) {
  SJd rt;
  rt.sz[0][0].push_back((z == 0 ? 499122177 : 1));
  rt.sz[1][0].push_back(1);
  rt.sz[0][1].push_back(0);
  rt.sz[0][1].push_back(1);
  rt.sz[1][1].push_back(0);
  rt.sz[1][1].push_back(1);
  return rt;
}
int main() {
  int n, m = 0;
  getww(2097152);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &sz[i]);
  for (int i = 0; i < n; i += sz[i]) {
    int t = sz[i];
    if (i + t > n) {
      printf("0");
      return 0;
    }
    for (int j = i + 1; j < i + t; j++) {
      if (sz[j] != t) {
        printf("0");
        return 0;
      }
    }
    ss[m++] = (t > 1);
  }
  jc[0] = mi[0] = 1;
  for (int i = 1; i <= m; i++) {
    jc[i] = 1ll * i * jc[i - 1] % 998244353;
    mi[i] = 2ll * mi[i - 1] % 998244353;
  }
  for (int i = 0; i < m; i++) zt[i] = caljz(ss[i]);
  int ans = 0;
  SJd rt = fzntt(0, m - 1);
  vector<int> ve = rt.sz[0][0];
  for (int i = 0; i < ve.size(); i++) {
    int z = 1ll * jc[m - i] * mi[m - i] % 998244353;
    if (i & 1) z = (998244353 - z) % 998244353;
    ans = (ans + 1ll * z * ve[i]) % 998244353;
  }
  printf("%d", ans);
  return 0;
}

### Prompt

Your task is to create a cpp solution to 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>
using namespace std;
int ksm(int a, int b) {
  int jg = 1;
  while (b > 0) {
    if (b & 1) jg = 1ll * jg * a % 998244353;
    a = 1ll * a * a % 998244353;
    b >>= 1;
  }
  return jg;
}
int w0[2097152 << 1], w1[2097152 << 1];
void getww(int n) {
  for (int h = 2; h <= n; h <<= 1) {
    int wn = ksm(3, (998244353 - 1) / h);
    for (int i = 0, w = 1; i < (h >> 1); i++, w = 1ll * w * wn % 998244353)
      w0[i + (h >> 1)] = w;
    wn = ksm(332748118, (998244353 - 1) / h);
    for (int i = 0, w = 1; i < (h >> 1); i++, w = 1ll * w * wn % 998244353)
      w1[i + (h >> 1)] = w;
  }
}
void NTT(int sz[2097152], int n, int lx) {
  for (int i = 0, t = 0; i < n; i++) {
    if (i < t) {
      int o = sz[i];
      sz[i] = sz[t];
      sz[t] = o;
    }
    for (int j = (n >> 1); (t ^= j) < j; j >>= 1)
      ;
  }
  for (int h = 2; h <= n; h <<= 1) {
    for (int i = 0; i < n; i += h) {
      for (int j = 0; j < (h >> 1); j++) {
        int w = (lx == 0 ? w0[j + (h >> 1)] : w1[j + (h >> 1)]);
        int z = 1ll * w * sz[i + j + (h >> 1)] % 998244353;
        sz[i + j + (h >> 1)] = (sz[i + j] - z + 998244353) % 998244353;
        sz[i + j] = (sz[i + j] + z) % 998244353;
      }
    }
  }
  if (lx == 1) {
    int nn = ksm(n, 998244353 - 2);
    for (int i = 0; i < n; i++) sz[i] = 1ll * sz[i] * nn % 998244353;
  }
}
int F[2097152], G[2097152];
vector<int> mul(const vector<int>& a, const vector<int>& b) {
  vector<int> rt;
  int le = 1, na = a.size(), nb = b.size();
  while (le < na + nb) le <<= 1;
  for (int i = 0; i < le; i++) F[i] = G[i] = 0;
  for (int i = 0; i < na; i++) F[i] = a[i];
  for (int i = 0; i < nb; i++) G[i] = b[i];
  NTT(F, le, 0);
  NTT(G, le, 0);
  for (int i = 0; i < le; i++) F[i] = 1ll * F[i] * G[i] % 998244353;
  NTT(F, le, 1);
  for (int i = 0; i < na + nb - 1; i++) rt.push_back(F[i]);
  return rt;
}
vector<int> add(const vector<int>& a, const vector<int>& b) {
  vector<int> rt;
  int na = a.size(), nb = b.size();
  rt.resize(na > nb ? na : nb);
  for (int i = 0; i < na; i++) rt[i] = a[i];
  for (int i = 0; i < nb; i++) rt[i] = (rt[i] + b[i]) % 998244353;
  return rt;
}
struct SJd {
  vector<int> sz[2][2];
};
SJd operator*(const SJd& a, const SJd& b) {
  SJd rt;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++)
        rt.sz[i][j] = add(rt.sz[i][j], mul(a.sz[i][k], b.sz[k][j]));
    }
  }
  return rt;
}
SJd zt[100010];
SJd fzntt(int l, int r) {
  if (l == r) return zt[l];
  int m = (l + r) >> 1;
  return fzntt(l, m) * fzntt(m + 1, r);
}
int sz[100010], ss[100010], jc[100010], mi[100010];
SJd caljz(int z) {
  SJd rt;
  rt.sz[0][0].push_back((z == 0 ? 499122177 : 1));
  rt.sz[1][0].push_back(1);
  rt.sz[0][1].push_back(0);
  rt.sz[0][1].push_back(1);
  rt.sz[1][1].push_back(0);
  rt.sz[1][1].push_back(1);
  return rt;
}
int main() {
  int n, m = 0;
  getww(2097152);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &sz[i]);
  for (int i = 0; i < n; i += sz[i]) {
    int t = sz[i];
    if (i + t > n) {
      printf("0");
      return 0;
    }
    for (int j = i + 1; j < i + t; j++) {
      if (sz[j] != t) {
        printf("0");
        return 0;
      }
    }
    ss[m++] = (t > 1);
  }
  jc[0] = mi[0] = 1;
  for (int i = 1; i <= m; i++) {
    jc[i] = 1ll * i * jc[i - 1] % 998244353;
    mi[i] = 2ll * mi[i - 1] % 998244353;
  }
  for (int i = 0; i < m; i++) zt[i] = caljz(ss[i]);
  int ans = 0;
  SJd rt = fzntt(0, m - 1);
  vector<int> ve = rt.sz[0][0];
  for (int i = 0; i < ve.size(); i++) {
    int z = 1ll * jc[m - i] * mi[m - i] % 998244353;
    if (i & 1) z = (998244353 - z) % 998244353;
    ans = (ans + 1ll * z * ve[i]) % 998244353;
  }
  printf("%d", ans);
  return 0;
}

```