#include<bits/stdc++.h>
using namespace std;
const int sz=6e5+7;
const int mod=998244353;
int n,t;
int ans;
int cnt;
int pos;
int sum;
bool flag;
int a[sz];
int fac[sz];
int st[sz],ed[sz];
int main(){
	scanf("%d",&n);
	fac[0]=1;
	for(int i=1;i<=n;i++) fac[i]=1ll*i*fac[i-1]%mod;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[n+i]=a[i];
	}
	pos=2;
	while(a[pos]==a[pos-1]) ++pos;
	if(pos==2*n+1&&a[1]!=1) return printf("0\n"),0;
	if(pos==2*n+1) return printf("%d\n",fac[n]),0;
	for(int i=pos;i<pos+n;i++){
		if(a[i]!=a[i-1]){
			if(st[a[i]]) flag=1;
			st[a[i]]=i;
		}
		if(a[i]!=a[i+1]) ed[a[i]]=i;
	}
	if(flag) return printf("0\n"),0;
	
	t=ed[1]-st[1];
	for(int i=1;i<=n;i++) if(ed[i]-st[i]>t) flag=1;
	if(flag) return printf("0\n"),0;
	
	ans=1;
	cnt=1,sum=t+1;
	for(int i=2;i<=n;i++){
		if(!st[i]) { ans=1ll*(sum-t*cnt-i+1)*ans%mod;continue; }
		int k1=a[st[i]-1]<i,k2=a[ed[i]+1]<i;
		if(k1&&k2) ans=1ll*(st[i]-(ed[i]-t)+1)*ans%mod,cnt--;
		if(!k1&&!k2) cnt++;
		if(!k1&&!k2&&ed[i]-st[i]!=t) ans=0;
		sum+=ed[i]-st[i]+1;
	}
	printf("%d\n",ans);
}