Problem: 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

Solution (in CPP):

Solution:
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"

using namespace std;

//const long long int MOD = 1000000007;
const int MOD = 998244353;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N);
	vector<int>w(N);
	for (auto &i : v)cin >> i;
	bool flag = true;
	L = -1;
	for (int i = N - 1; i >= 0; i--) {
		if (v[i] == 1) {
			if (flag) {
				L = i;
			}
		}
		else {
			if (L != -1)flag = false;
		}
	}
	if (L == -1) {
		cout << 0 << endl;
		return 0;
	}
	for (int i = 0; i < N; i++) {
		w[i] = v[(i + L) % N];
	}
	vector<int>l(N + 1, -1);
	vector<int>r(N + 1, -1);
	for (int i = 0; i < N; i++) {
		if (r[w[i]] == -1) {
			r[w[i]] = i;
			l[w[i]] = i;
		}
		else if (r[w[i]] == i - 1) {
			r[w[i]] = i;
		}
		else {
			cout << 0 << endl;
			return 0;
		}
	}
	w.push_back(1);
	long long int ans = 1;
	if (r[1] == N - 1) {
		for (int i = 2; i <= N; i++) {
			ans *= i;
			ans %= MOD;
		}
		cout << ans << endl;
		return 0;
	}
	int by = 0;
	for (int i = 2; i <= N; i++) {
		if (l[i] == -1) {
			ans *= by;
			by--;
			ans %= MOD;
		}
		else if (r[i] - l[i] > r[1] - l[1]) {
			cout << 0 << endl;
			return 0;
		}
		else if (w[l[i] - 1] > i&&w[r[i] + 1] > i&&r[i] - l[i] < r[1] - l[1]) {
			cout << 0 << endl;
			return 0;
		}
		else if (r[i] - l[i] <= r[1] - l[1]) {
			if (w[l[i] - 1] < i&&w[r[i] + 1] < i) {
				ans *= r[1] - l[1] - (r[i] - l[i]) + 1;
				by += r[1] - l[1] + 1 + r[i] - l[i] + 1 - 2;
				ans %= MOD;
			}
			else if(!(w[l[i] - 1] > i&&w[r[i] + 1] > i)){
				by += r[i] - l[i];
			}
		}
	}
	cout << ans << endl;
	return 0;
}