#include <cstdio>
using ll = long long;
constexpr int MOD = 998244353;
struct data { int l, r; } rg[300005];
int arr[300005];
int main()
try
{
	// freopen("TK1-F.in", "r", stdin);
	int n, lst = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		rg[i].l = rg[i].r = -1;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", arr + i);
		if (i && arr[i] != arr[i - 1])
		{
			auto &it = rg[arr[i - 1]];
			if (~it.r)
				throw false;
			it = {lst, i - 1};
			lst = i;
		}
	}
	if (!lst)
	{
		if (arr[0] != 1)
			throw false;
		ll ans = 1;
		for (int i = 1; i <= n; i++)
			ans = ans * i % MOD;
		printf("%lld\n", ans);
		return 0;
	}
	arr[n] = arr[0];
	if (arr[n - 1] == arr[n])
		rg[arr[n]].l = lst;
	else
	{
		auto &it = rg[arr[n - 1]];
		if (~it.r)
			throw false;
		it = {lst, n - 1};
	}
	if (-1 == rg[1].r)
		throw false;
	auto calc_len = [&] (data & a) { return a.r - a.l + 1 + (a.l <= a.r ? 0 : n); };
	int k = calc_len(rg[1]), rem = 0;
	ll ans = 1;
	for (int i = 1; i <= n; i++)
	{
		if (-1 == rg[i].r)
		{
			ans = ans * rem-- % MOD;
			continue;
		}
		int l = rg[i].l, r = rg[i].r, len = calc_len(rg[i]);
		bool x = (arr[r + 1 - (r + 1 == n ? n : 0)] < i);
		bool y = (arr[l - 1 + (!l ? n : 0)] < i);
		if (len > k || (!x && !y && len != k))
			throw false;
		if (x && y)
		{
			rem += k + len - 2;
			ans = ans * (k - len + 1) % MOD;
		}
		else if (x || y)
			rem += len - 1;
	}
	printf("%lld\n", ans);
	return 0;
}
catch (bool)
{
	puts("0");
	return 0;
}
