#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

void claim(int x){
	if(!x) puts("0"),exit(0);
}

const int N=300005,mod=998244353;
int a[N],b[N],c[N],l[N],r[N],vis[N];
int n,p,len,ans=1,rem;

int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		if(!p&&a[i]==1) p=i;
	}
	if(a[n]==1){
		p=n;
		while(a[p-1]==1) p--;
	}
	claim(p);
	rep(i,1,p-1) b[i]=a[i];
	rep(i,1,n-p+1) a[i]=a[i+p-1];
	rep(i,1,p-1) a[i+n-p+1]=b[i];
	/*
	puts("===");
	rep(i,1,n) printf("%d ",a[i]);
	puts("\n---");
	*/
	rep(i,1,n){
		c[a[i]]++;
		if(!l[a[i]]) l[a[i]]=i;
		r[a[i]]=i;
	}
	rep(i,1,n) claim(!c[i]||c[i]==r[i]-l[i]+1&&c[i]<=c[1]);
	len=c[1];
	if(len==n){
		int ans=1;
		rep(i,1,n) ans=(ll)ans*i%mod;
		cout<<ans<<endl;
		return 0;
	}
	rep(i,2,n){
		if(!c[i]){
			claim(rem);
			ans=(ll)ans*(rem--)%mod;
			continue;
		}
		int L=(a[l[i]-1]<i),R=(a[r[i]+1]<i);
		if(L&&R){
			ans=(ll)ans*(len-c[i]+1)%mod;
			rem+=c[i]+len-2;
		}
		else if(L||R) rem+=c[i]-1;
		else claim(c[i]==len);
	}
	cout<<ans<<endl;
	return 0;
}