#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, mod = 998244353;
int n, tot, a[N], b[N], c[N], vis[N], t[N << 2], lz[N << 2];
int sum[N];
void build(int l, int r, int k) {
  if (l == r) {
    t[k] = b[l];
    return;
  }
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
}
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) {
    lz[k] = max(lz[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) {
  t[k] = max(t[k], lz[k]);
  if (l == r) {
    b[l] = max(b[l], t[k]);
    return;
  }
  int m = l + r >> 1;
  lz[k << 1] = max(lz[k << 1], lz[k]);
  lz[k << 1 | 1] = max(lz[k << 1 | 1], lz[k]);
  push(l, m, k << 1);
  push(m + 1, r, k << 1 | 1);
}
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;
  int t = 1;
  for (int i = 2; i <= n; i++) {
    if (b[i] == b[i - 1])
      t++;
    else {
      if (b[i] > b[i - 1]) c[i - len] = b[i - 1];
      t = 1;
    }
    if (t > len) flag = false;
  }
  if (t == len) c[n - len + 1] = b[n];
  memset(b, 0, sizeof(b));
  build(1, n, 1);
  for (int i = 1; i <= n; i++)
    if (c[i]) {
      if (i >= len)
        fix(1, n, 1, i - len + 1, i, c[i]);
      else {
        fix(1, n, 1, 1, i, c[i]);
        fix(1, n, 1, n - (len - i) + 1, n, c[i]);
      }
      if (i + len - 1 <= n)
        fix(1, n, 1, i, i + len - 1, c[i]);
      else {
        fix(1, n, 1, 1, n, c[i]);
        fix(1, n, 1, 1, len - (n - i + 1), c[i]);
      }
    }
  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++)
    if (c[i])
      sum[c[i]] = 0;
    else
      vis[b[i] + 1]++;
  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 (sum[1] != 0) flag = false;
  if (!flag) ans = 0;
  printf("%lld\n", ans);
}
