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
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

using namespace std;

const int mod = 998244353;

void add(int& a, int b) {
  a += b;
  if (a >= mod) a -= mod;
  if (a < 0) a += mod;
}

int mult(int a, int b) {
  return a * (ll)b % mod;
}

int bp(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

const int N = 100007;
int fact[N];

struct Ans {
  vector<int> g[4];
};

class cn {
public:
  db x, y;
  cn(){}
  cn(db xx, db yy): x(xx), y(yy) {}
  cn(db xx): x(xx), y(0) {}
  db real() { return x; }
  void operator /= (double f) { x /= f; y /= f; }
};

cn operator + (cn a, cn b) { return cn(a.x + b.x, a.y + b.y); }
cn operator - (cn a, cn b) { return cn(a.x - b.x, a.y - b.y); }
cn operator * (cn a, cn b) { return cn(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }

class FFT {
public:
  constexpr static db pi = acos(-1.0);
  const static int MAX_SIZE = 1 << 19;

  //#define cn complex<db>

  int n;
  cn a[MAX_SIZE * 2 + 7], b[MAX_SIZE * 2 + 7];

  int getReverse(int a, int k){
    int ans = 0;
    for (int i = 0; i < k; i++) if ((a >> i) & 1) ans ^= (1 << (k - i - 1));
    return ans;
  }

  void fft(cn *a, int type){
    int k = -1;
    for (int i = 0; i < 25; i++) if ((n >> i) & 1){ 
      k = i;
      break;
    }
    for (int i = 0; i < n; i++){
      int j = getReverse(i, k);
      if (i < j) swap(a[i], a[j]);
    }
    for (int len = 2; len <= n; len *= 2){
      cn w(cos(2 * pi / (db)len), sin(2 * pi / (db)len) * type);
      for (int i = 0; i < n; i += len){
        cn g = cn(1, 0);
        for (int j = 0; j < len / 2; j++){
          cn x = a[i + j];
          cn y = a[i + j + len / 2] * g;
          a[i + j] = x + y;
          a[i + j + len / 2] = x - y;
          g = g * w;
        }
      }
    }
    if (type == -1) for (int i = 0; i < n; i++) a[i] /= n; 
  }

  vector<int> mult(vector<int> &w1, vector<int> &w2){
    n = 1;
    while(n < w1.size() + w2.size()) n <<= 1;
    std::copy(w1.begin(), w1.end(), a);
    std::copy(w2.begin(), w2.end(), b);
    std::fill(a + w1.size(), a + n, 0);
    std::fill(b + w2.size(), b + n, 0);
    fft(a, 1);
    fft(b, 1);
    for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
    fft(a, -1);
    vector<int> ans(n);
    for (int i = 0; i < n; i++) ans[i] = floor((db)a[i].real() + 0.5);
    while(ans.size() && ans.back() == 0) ans.pop_back();
    return ans;
  }
};

//////////// (0,0) (0,1) (1,0) (1,1)

vector<int> add(vector<int> a, vector<int> b) {

  vector<int> c(max(a.size(), b.size()), 0);
  for (int i = 0; i < a.size(); ++i) add(c[i], a[i]);
  for (int i = 0; i < b.size(); ++i) add(c[i], b[i]);

  return c;

}

FFT fft;

Ans solve(vector<int> &v, int l, int r) {

  if (l == r) {
    vector<int> a = {0, (v[l] > 1) + 1};
    vector<int> b = {0, 2};
    vector<int> c = {1};
    vector<int> d = {1};
    Ans ans;
    ans.g[0] = a, ans.g[1] = b, ans.g[2] = c, ans.g[3] = d;
    return ans;
  }

  int mid = (l+r)/2;
  auto A = solve(v, l, mid), B = solve(v, mid + 1, r);

  Ans ans;
  ans.g[0] = add(fft.mult(A.g[0], B.g[0]), fft.mult(A.g[1], B.g[2]));
  ans.g[1] = add(fft.mult(A.g[0], B.g[1]), fft.mult(A.g[1], B.g[3]));
  ans.g[2] = add(fft.mult(A.g[2], B.g[0]), fft.mult(A.g[3], B.g[2]));
  ans.g[3] = add(fft.mult(A.g[2], B.g[1]), fft.mult(A.g[3], B.g[3]));

  //cout << l << " " << r << " " << ans.g[0].size() << endl;

  return ans;


}

int main(){
#ifdef LOCAL
	freopen("N_input.txt", "r", stdin);
	//freopen("N_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

  fact[0] = 1;
  for (int i = 1; i < N; ++i) fact[i] = mult(i, fact[i - 1]);

  int n;
  cin >> n;

  vector<int> v(n);
  for (auto &x : v) cin >> x;

  int start = 0;
  vector<int> arr;

  while (start < n) {
    if (start + v[start] > n) {
      cout << 0;
      exit(0);
    }
    for (int i = 0; i < v[start]; ++i) {
      if (v[i + start] != v[start]) {
        cout << 0;
        exit(0);
      }
    }

    arr.pb(v[start]);
    start += v[start];
  }

  int ans = 0;

  auto res = solve(arr, 0, arr.size() - 1).g[0];
  for (int i = 0; i <= arr.size(); ++i) {
    int viol = arr.size() - i;
    if (viol % 2 == 0) add(ans, mult(fact[i], res[i]));
    else add(ans, -mult(fact[i], res[i]));
  }

  cout << ans;

}
