You are given a sequence of N integers: A_1,A_2,...,A_N.

Find the number of permutations p_1,p_2,...,p_N of 1,2,...,N that can be changed to A_1,A_2,...,A_N by performing the following operation some number of times (possibly zero), modulo 998244353:

* For each 1\leq i\leq N, let q_i=min(p_{i-1},p_{i}), where p_0=p_N. Replace the sequence p with the sequence q.

Constraints

* 1 \leq N \leq 3 × 10^5
* 1 \leq A_i \leq N
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1
:
A_N


Output

Print the number of the sequences that satisfy the condition, modulo 998244353.

Examples

Input

3
1
2
1


Output

2


Input

5
3
1
4
1
5


Output

0


Input

8
4
4
4
1
1
1
2
2


Output

24


Input

6
1
1
6
2
2
2


Output

0
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

template <unsigned int MOD>
struct ModInt {
	using uint = unsigned int;
	using ull = unsigned long long;
	using M = ModInt;

	uint v;

	ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
	M& set_norm(uint _v) { //[0, MOD * 2)->[0, MOD)
		v = (_v < MOD) ? _v : _v - MOD;
		return *this;
	}

	explicit operator bool() const { return v != 0; }
	M operator+(const M& a) const { return M().set_norm(v + a.v); }
	M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
	M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
	M operator/(const M& a) const { return *this * a.inv(); }
	M& operator+=(const M& a) { return *this = *this + a; }
	M& operator-=(const M& a) { return *this = *this - a; }
	M& operator*=(const M& a) { return *this = *this * a; }
	M& operator/=(const M& a) { return *this = *this / a; }
	M operator-() const { return M() - *this; }
	M& operator++(int) { return *this = *this + 1; }
	M& operator--(int) { return *this = *this - 1; }

	M pow(ll n) const {
		if (n < 0) return inv().pow(-n);
		M x = *this, res = 1;
		while (n) {
			if (n & 1) res *= x;
			x *= x;
			n >>= 1;
		}
		return res;
	}

	M inv() const {
		ll a = v, b = MOD, p = 1, q = 0, t;
		while (b != 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(p -= t * q, q);
		}
		return M(p);
	}

	bool operator==(const M& a) const { return v == a.v; }
	bool operator!=(const M& a) const { return v != a.v; }
	friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
	static int get_mod() { return MOD; }	
};

using Mint = ModInt<998244353>;

const int maxv = 300010;

V<Mint> fact(maxv), ifact(maxv), inv(maxv);

void init() {
	fact[0] = 1;
	for (int i = 1; i < maxv; ++i) {
		fact[i] = fact[i-1] * i;
	}

	ifact[maxv-1] = fact[maxv-1].inv();

	for (int i = maxv - 2; i >= 0; --i) {
		ifact[i] = ifact[i+1] * (i+1);
	}

	for (int i = 1; i < maxv; ++i) {
		inv[i] = ifact[i] * fact[i-1];
	}
}

Mint comb(int n, int r) {
	if (n < 0 || r < 0 || r > n) return Mint(0);
	return fact[n] * ifact[r] * ifact[n-r];
}

void ng() {
	puts("0");
	exit(0);
}

int main() {
	init();

	int N; cin >> N;
	V<int> A(N);

	rep(i, N) {
		cin >> A[i];
		--A[i];
	}

	int maxi;

	{
		maxi = *max_element(ALL(A));
		rep(i, N-1) {
			if (A[i] == maxi && A[i+1] != maxi) {
				V<int> B;
				for (int j = i+1; j < N; ++j) {
					B.pb(A[j]);
				}
				for (int j = 0; j <= i; ++j) {
					B.pb(A[j]);
				}
				A = B;

				break;
			}
		}

		dump(A);
	}

	V<bool> used(N);
	V<pii> pos(N);

	int s = 0;
	int nums = 0;

	while (s < N) {
		int t = s;
		while (t < N && A[t] == A[s]) {
			++t;
		}
		if (used[A[s]]) {
			ng();
		}
		nums++;
		pos[A[s]] = mp(s, t);
		used[A[s]] = true;
		s = t;
	}

	if (nums == 1) {
		if (A[0] == 0) {
			cout << fact[N] << endl;
		} else {
			ng();
		}
		return 0;
	}

	if (!used[0]) {
		ng();
	}

	int T = pos[0].se - pos[0].fi;

	for (auto p : pos) {
		if (p.se - p.fi > T) {
			ng();
		}
	}

	dump(pos);

	dump(T);

	auto getpos = [&](int p) {
		if (p < 0) p += N;
		if (p >= N) p -= N;
		return p;		
	};

	auto get = [&](int p) {
		return A[getpos(p)];
	};

	auto calc = [&](pii rng) {
		return max(0, rng.se - rng.fi - T + 1);
	};

	set<int> rems({-1, N});

	rep(i, N) {
		rems.insert(i);
	}

	int free = 0;
	Mint ans = 1;

	rep(i, N) {
		if (used[i]) {
			int l, r;
			tie(l, r) = pos[i];
			if (get(l-1) > i && get(r) > i && r - l != T) {
				ng();
			} else if (get(l-1) < i && get(r) < i) {
				if (i == maxi) {
					ans *= T - (r - l) + 1;
				} else {
					auto itl = rems.lower_bound(l), itr = rems.lower_bound(r);
					if (*itr - T < l) {
						ans = 0;
					} else {
						--itl;
						int tol = max(*itl + 1, r - T);

						dump(tol);
						int w = max(0, l - tol + 1);
						dump(w);
						ans *= w;
					}
				}
			}

			for (int k = l; k < r; ++k) {
				rems.erase(k);
			}

			auto itl = rems.lower_bound(l), itr = itl;
			--itl;

			free -= calc(mp(*itl + 1, l));
			free -= calc(mp(r, *itr));
			free += calc(mp(*itl + 1, *itr));
		} else {
			if (rems.size() == 2) {
				ans *= (N - i);
				continue;
			}
			dump(mp(i, free));
			ans *= Mint(free - i);
		}
		dump(mp(i, ans));
	}

	cout << ans << endl;

	return 0;
}