#include <iostream>
#include <vector>
#include <set>
#define llint long long
#define inf 1000000000
#define mod 998244353

using namespace std;

struct UnionFind{
	int size;
	vector<int> parent;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};

llint n;
llint a[300005];
llint l[300005], r[300005];
llint R[300005];
UnionFind uf(300005);
set<llint> S;

int main(void)
{
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i];
	
	for(int i = 0; i < n-1; i++){
		if(a[i] == a[i+1]) uf.unite(i, i+1);
	}
	if(a[0] == a[n-1]) uf.unite(0, n-1);
	
	llint len = 0;
	for(int i = 1; i <= n; i++) R[i] = -1;
	for(int i = 0; i < n; i++){
		if(R[a[i]] != -1 && R[a[i]] != uf.root(i)){
			cout << 0 << endl;
			return 0;
		}
		R[a[i]] = uf.root(i);
		if(a[i] == 1) len++;
	}
	if(len == n){
		llint ans = 1;
		for(int i = 1; i <= n; i++) ans *= i, ans %= mod;
		cout << ans << endl;
		return 0;
	}
	
	for(int i = 1; i <= n; i++) l[i] = inf, r[i] = -inf;
	for(llint i = 0; i < n; i++){
		l[a[i]] = min(l[a[i]], i);
		r[a[i]] = max(r[a[i]], i);
	}
	if(a[0] == a[n-1]){
		for(int i = 0; i < n; i++){
			if(a[i] != a[0]){
				r[a[0]] = i-1;
				break;
			}
		}
		for(int i = n-1; i >= 0; i--){
			if(a[i] != a[n-1]){
				l[a[n-1]] = i+1;
				break;
			}
		}
		if(l[a[0]] > r[a[0]]) r[a[0]] += n;
	}
	//for(int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
	
	for(int i = 1; i <= n; i++){
		if(l[i] == inf) continue;
		if(r[i]-l[i]+1 > len){
			cout << 0 << endl;
			return 0;
		}
	}
	
	llint ans = 1, valid = 0;
	for(int i = l[1]; i <= r[1]; i++) S.insert(i%n);
	for(int i = 2; i <= n; i++){
		if(l[i] == inf){
			if(valid <= 0){
				cout << 0 << endl;
				return 0;
			}
			ans *= valid, ans %= mod;
			valid--;
			continue;
		}
		bool lflag = S.count((l[i]-1+n)%n), rflag = S.count((r[i]+1)%n);
		if(!lflag && !rflag){
			if(len != r[i]-l[i]+1){
				cout << 0 << endl;
				return 0;
			}
		}
		else if(!lflag || !rflag) valid += r[i]-l[i];
		else{
			ans *= len - (r[i]-l[i]+1) + 1, ans %= mod;
			valid += len-1+(r[i]-l[i]+1)-1;
		}
		for(int j = l[i]; j <= r[i]; j++) S.insert(j%n);
		//cout << i << " " << S.size() << " " << valid << " " << ans << endl;
	}
	cout << ans << endl;
	
	return 0;
}