#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 998244353;
int fac[300001], inv[300001];
int a[300000];
int minx[300000], maxx[300000];
int comb(int a, int b) {
  if (b < 0 || b > a) return 0;
  return (long)fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int solve(int n) {
  int n0 = count(a, a + n, 0);
  if (n0 == 0) return 0;
  if (n0 == n) {
    int ans = 1;
    for (int j = 1; j <= n; j++) ans = (long)ans * j % mod;
    return ans;
  }
  if (a[0] != 0) {
    rotate(a, find(a, a + n, 0), a + n);
  } else {
    int j = 0;
    while (a[j] == 0) j++;
    rotate(a, a + (n - (n0 - j)), a + n);
  }
  if (find(a + n0, a + n, 0) != a + n) return 0;
  fill_n(minx, n, n);
  fill_n(maxx, n, -1);
  for (int i = 0; i < n; i++) {
    minx[a[i]] = min(minx[a[i]], i);
    maxx[a[i]] = max(maxx[a[i]], i);
  }
  int ans = 1, num = 0;
  for (int i = n - 1; i >= 1; i--) {
    num++;
    if (maxx[i] < minx[i]) continue;
    if (maxx[i] - minx[i] >= n0) return 0;
    if (count(a + minx[i], a + maxx[i], i) != maxx[i] - minx[i]) return 0;
    int l = maxx[i] - (n0 - 1), r = minx[i];
    int ll = minx[i] - (n0 - 1), rr = maxx[i];
    if (a[minx[i]] > i) {
      l = max(l, minx[i]);
      ll = max(ll, minx[i]);
    }
    if (maxx[i] + 1 < n && a[maxx[i] + 1] > i) {
      r = min(r, maxx[i] - (n0 - 1));
      rr = min(rr, maxx[i] - (n0 - 1));
    }
    if (l > r) return 0;
    ans = (long)ans * (r - l + 1) % mod;
    num--;
    ans = (long)ans * comb(num, rr - ll) % mod;
    ans = (long)ans * fac[rr - ll] % mod;
    num -= (rr - ll);
  }
  return ans;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= 300000; i++) fac[i] = (long)fac[i - 1] * (i) % mod;
  inv[300000] = 988693096;
  for (int i = 300000; i >= 1; i--) inv[i - 1] = (long)inv[i] * i % mod;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", a + i), a[i]--;
  printf("%d\n", solve(n));
}
