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<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=998244353;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,i;cin>>n;
	vector<int>mA(n);
	for(i=0;i<n;i++){cin>>mA[i];}
	//まずは1のスタート地点を見つけて、ずらす
	int stat=-1;
	for(i=0;i<n-1;i++){
		if(mA[i]!=1&&mA[i+1]==1){stat=i+1;break;}
	}
	if(mA[n-1]!=1&&mA[0]==1){stat=0;}
	
	if(stat==-1){
		if(mA[0]!=1){cout<<0<<endl;}//1がない
		else{//1しかない
			llint aaa=1;
			for(i=1;i<=n;i++){aaa*=i;aaa%=mod;}
			cout<<aaa<<endl;
		}
		return 0;
	}
	vector<int>A(n+1);
	for(i=0;i<n;i++){A[i]=mA[(i+stat)%n];}
	A[n]=A[0];
	vector<int>L(n+1,-1);
	vector<int>R(n+1,-1);
	for(i=0;i<n;i++){
		if(L[A[i]]==-1){L[A[i]]=i;}
		R[A[i]]=i;
	}
	
	int kaz=0;
	for(i=1;i<=n;i++){if(L[i]!=-1){kaz+=R[i]+1-L[i];}}
	if(kaz!=n){cout<<0<<endl;return 0;}
	int sla=R[1]-L[1]+1;
	llint ans=1;
	vector<int>ren(n,-1);
	ren[0]=sla;
	ren[sla-1]=sla;
	int oke=0;
	for(i=2;i<=n;i++){
		
		if(L[i]==-1){ans*=oke;ans%=mod;oke--;continue;}
		int nga=R[i]-L[i]+1;
		if(nga>sla){cout<<0<<endl;return 0;}
		int zL=L[i],zR=R[i];
		
			
		if(ren[L[i]-1]!=-1){
			oke-=max(0,ren[L[i]-1]+1-sla);
			zL=L[i]-ren[L[i]-1];
		}
		if(ren[(R[i]+1)%n]!=-1){
			oke-=max(0,ren[(R[i]+1)%n]+1-sla);
			zR=R[i]+ren[(R[i]+1)%n];
		}
		int nn=min(n,zR-zL+1);
		oke+=max(0,nn+1-sla);
		if(ren[L[i]-1]+nga==n){oke=n-i+1;}
		ren[(zL+n)%n]=nn;ren[(zR+n)%n]=nn;
		//zL...L...R...zR
		//zLやzRは範囲外かもしれない
		ans*=max(0,min(L[i]+sla-1,zR)-max(R[i],zL+sla-1)+1);
		ans%=mod;oke--;
	}
	cout<<ans<<endl;
	return 0;
}