#include <bits/stdc++.h>
using namespace std;
struct tk {
  long long l, r;
  bool operator<(const tk& y) const { return r < y.r || (r == y.r && l < y.l); }
} h;
long long rev, n, i, x[300005], j, seq[300005],
    tot = 0, y[300005], p, fac[300005], invfac[300005], cnt = 0, l[300005],
    r[300005], vis[300005], pos, ans;
const long long mod = 998244353;
map<tk, long long> mp;
map<tk, long long>::iterator it;
long long pow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  scanf("%lld", &n);
  fac[0] = ans = 1;
  for (i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;
  invfac[n] = pow(fac[n], mod - 2);
  for (i = n - 1; i >= 0; i--) invfac[i] = invfac[i + 1] * (i + 1) % mod;
  for (i = 1; i <= n; i++) scanf("%lld", &x[i]);
  for (i = 1; i <= n; i++)
    if (x[i] > 1) break;
  if (i > n) {
    printf("%lld", fac[n]);
    return 0;
  }
  x[0] = x[n];
  for (i = 1; i <= n; i++) {
    if (x[i] != x[i - 1]) {
      vis[x[i]]++;
      if (x[i] == 1) l[x[i]] = i;
    }
    if (x[i] == 1) cnt++;
    if (vis[x[i]] > 1) {
      printf("0\n");
      return 0;
    }
  }
  mp[(tk){1, n - 1}] = 1;
  j = 1;
  pos = (l[1] == n) ? 1 : l[1] + 1;
  p = cnt - 1;
  while (j < n) {
    y[j++] = x[pos];
    pos = (pos == n) ? 1 : pos + 1;
  }
  cnt = 0;
  for (i = 1; i <= n; i++) {
    if (!l[y[i]]) l[y[i]] = i;
    r[y[i]] = i;
  }
  for (i = 2; i <= n; i++) {
    if (!l[i])
      seq[++tot] = cnt;
    else {
      it = mp.lower_bound((tk){-1, r[i]});
      h = it->first;
      if (l[i] > h.l + p) {
        if (r[i] - l[i] + 1 != p + 1) {
          printf("0\n");
          return 0;
        } else {
          mp.erase(h);
          mp[(tk){h.l, l[i] - 1}] = 1;
          if (r[i] < h.r)
            mp[(tk){l[i] + 1, h.r}] = 1;
          else
            cnt += h.r - l[i];
        }
      } else {
        if (r[i] - l[i] + 1 > p + 1) {
          printf("0\n");
          return 0;
        }
        if (r[i] < h.r) {
          cnt += r[i] - p - h.l;
          mp.erase(h);
          mp[(tk){r[i] - p + 1, h.r}] = 1;
        } else {
          ans = ans * (p + 1 - r[i] + l[i]) % mod;
          cnt += h.r - h.l;
          mp.erase(h);
        }
      }
    }
  }
  for (i = 1; i <= tot; i++) {
    if (seq[i] - i + 1 > 0)
      ans = ans * (seq[i] - i + 1) % mod;
    else {
      printf("0\n");
      return 0;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
