#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"

using namespace std;

//const long long int MOD = 1000000007;
const long long int MOD = 998244353;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N);
	vector<int>w(N);
	for (auto &i : v)cin >> i;
	bool flag = true;
	L = -1;
	for (int i = N - 1; i >= 0; i--) {
		if (v[i] == 1) {
			if (flag) {
				L = i;
			}
		}
		else {
			if (L != -1)flag = false;
		}
	}
	if (L == -1) {
		cout << 0 << endl;
		return 0;
	}
	for (int i = 0; i < N; i++) {
		w[i] = v[(i + L) % N];
	}
	vector<int>l(N + 1, -1);
	vector<int>r(N + 1, -1);
	for (int i = 0; i < N; i++) {
		if (r[w[i]] == -1) {
			r[w[i]] = i;
			l[w[i]] = i;
		}
		else if (r[w[i]] == i - 1) {
			r[w[i]] = i;
		}
		else {
			cout << 0 << endl;
			return 0;
		}
	}
	w.push_back(1);
	long long int ans = 1;
	if (r[1] == N - 1) {
		for (int i = 2; i <= N; i++) {
			ans *= i;
			ans %= MOD;
		}
		cout << ans << endl;
		return 0;
	}
	int by = 0;
	for (int i = 2; i <= N; i++) {
		if (l[i] == -1) {
			ans *= by;
			by--;
			ans %= MOD;
		}
		else if (r[i] - l[i] > r[1] - l[1]) {
			cout << 0 << endl;
			return 0;
		}
		else if (w[l[i] - 1] > i&&w[r[i] + 1] > i&&r[i] - l[i] < r[1] - l[1]) {
			cout << 0 << endl;
			return 0;
		}
		else if (r[i] - l[i] <= r[1] - l[1]) {
			if (w[l[i] - 1] < i&&w[r[i] + 1] < i) {
				ans *= r[1] - l[1] - (r[i] - l[i]) + 1;
				by += r[1] - l[1] + 1 + r[i] - l[i] + 1 - 2;
				ans %= MOD;
			}
			else if(!(w[l[i] - 1] > i&&w[r[i] + 1] > i)){
				by += r[i] - l[i];
			}
		}
	}
	cout << ans << endl;
	return 0;
}
