#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, mod = 998244353;
int n, tot, sum[N], a[N], b[N], c[N], vis[N];
int t[N << 2];
void fix(int l, int r, int k, int x, int y, int v) {
  if (r < x || l > y) return;
  if (l >= x && r <= y) {
    t[k] = max(t[k], v);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x, y, v);
  fix(m + 1, r, k << 1 | 1, x, y, v);
}
void push(int l, int r, int k) {
  if (l == r) {
    b[l] = t[k];
    return;
  }
  int m = l + r >> 1;
  t[k << 1] = max(t[k << 1], t[k]);
  t[k << 1 | 1] = max(t[k << 1 | 1], t[k]);
  push(l, m, k << 1);
  push(m + 1, r, k << 1 | 1);
}
void solve(int l, int r, int v) {
  if (l >= 1 && r <= n)
    fix(1, n, 1, l, r, v);
  else if (l < 1 && r > n)
    fix(1, n, 1, 1, n, v);
  else if (l < 1) {
    fix(1, n, 1, 1, r, v);
    l--;
    fix(1, n, 1, n - l + 1, n, v);
  } else if (r > n) {
    fix(1, n, 1, l, n, v);
    r -= n;
    fix(1, n, 1, 1, r, v);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int p = 0;
  bool flag = true;
  for (int i = n; i >= 1; i--)
    if (a[i] == 1 && a[i - 1] != 1) {
      p = i;
      break;
    }
  if (!p) flag = false;
  for (int i = p; i <= n; i++) b[++tot] = a[i];
  for (int i = 1; i < p; i++) b[++tot] = a[i];
  for (int i = 1; i <= n; i++)
    if (b[i] != b[i - 1]) vis[b[i]]++;
  for (int i = 1; i <= n; i++)
    if (vis[i] >= 2) flag = false;
  int len = 0;
  for (int i = 1; i <= n; i++)
    if (b[i] == 1)
      len++;
    else
      break;
  if (len == n) {
    long long ans = 1;
    for (int i = 1; i <= n; i++) ans = ans * i % mod;
    printf("%lld\n", ans);
    return 0;
  }
  int t = 1;
  for (int i = 2; i <= n; i++) {
    if (b[i] == b[i - 1])
      t++;
    else {
      if (b[i] > b[i - 1] || t == len) c[i - len] = b[i - 1];
      solve(i - (t - 1), i - 1, b[i - 1] + 1);
      solve(i - len, i - t, b[i - 1]);
      solve(i - t - len + 1, i - len - 1, b[i - 1] + 1);
      t = 1;
    }
    if (t > len) flag = false;
  }
  if (t == len) c[n - len + 1] = b[n];
  if (t > 1) solve(n + 1 - (t - 1), n + 1 - 1, b[n] + 1);
  solve(n + 1 - len, n + 1 - t, b[n]);
  solve(n + 1 - t - len + 1, n + 1 - len - 1, b[n] + 1);
  push(1, n, 1);
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) sum[i] = 1;
  for (int i = 1; i <= n; i++) sum[c[i]] = 0;
  for (int i = 1; i <= n; i++)
    if (!c[i]) vis[b[i]]++;
  long long ans = 1;
  for (int i = n + 1; i >= 1; i--) {
    sum[i] += sum[i + 1];
    while (vis[i]) {
      if (!sum[i]) {
        flag = false;
        break;
      }
      ans = ans * sum[i] % mod;
      sum[i]--;
      vis[i]--;
    }
  }
  if (!flag) ans = 0;
  printf("%lld\n", ans);
}
