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 <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll mo=998244353;
int N;
int B[1303030];
int A[1303030];

vector<int> P[303030];
int L[303030];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i];
		A[i]--;
	}
	
	if(count(A,A+N,0)==0) return _P("0\n");
	if(count(A,A+N,0)==N) {
		ll ret=1;
		FOR(i,N) ret=ret*(i+1)%mo;
		cout<<ret<<endl;
		return;
	}
	
	FOR(i,N) if(A[i]==0 && A[(i+N-1)%N]>0) {
		FOR(j,2*N) B[j]=A[(i+j)%N];
		FOR(j,2*N) A[j]=B[j];
		FOR(j,N) P[A[j]].push_back(j);
		break;
	}
	
	ll ret=1;
	set<int> empty,unused;
	int numfree=0;
	
	empty.insert(4*N);
	FOR(i,N) empty.insert(i), empty.insert(i+N);
	
	FOR(i,N) {
		//cout<<"!"<<i<<" "<<numfree<<endl;
		L[i]=P[i].size();
		if(L[i]>L[0]) return _P("0\n");
		if(L[i]==0) {
			//cout<<i<<" "<<numfree<<endl;
			ret=ret*numfree%mo;
			numfree--;
		}
		else {
			if(P[i][0]+L[i]-1!=P[i].back()) return _P("0\n");
			FORR(p,P[i]) {
				empty.erase(p);
				empty.erase(N+p);
				unused.insert(p);
			}
			
			x=P[i][0];
			if(A[x+N-1]>i && A[x+L[i]]>i && L[i]!=L[0]) return _P("0\n");
			if(A[x+N-1]<i && A[x+L[i]]<i) ret=ret*(L[0]-L[i]+1)%mo;
			auto it=unused.lower_bound(P[i][0]-(L[0]-1));
			while(it!=unused.end() && *it<=P[i].back()) {
				auto it2=empty.lower_bound(*it);
				if(*it2-*it<L[0]) {
					it=unused.lower_bound(*it2);
				}
				else {
					numfree++;
					it=unused.erase(it);
				}
			}
			numfree--;
		}
	}
	cout<<ret<<endl;
	
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}