#include <bits/stdc++.h>
int l[200005], r[200005];
int a[200005];
int fix[200005];
const long long mod = 998244353;
long long getF(int n) {
  long long res = 1;
  for (long long i = 2; i <= n; i++) res = res * i % mod;
  return res;
}
int getLength(int l, int r, int n) {
  if (l < 1) l += n;
  if (l > n) l -= n;
  if (r < 1) r += n;
  if (r > n) r -= n;
  if (r >= l)
    return r - l + 1;
  else
    return n - l + 1 + r;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int p = 1, ok = 1;
  int cnt = 0;
  while (p <= n) {
    int np = p, u = a[p];
    while (np + 1 <= n && a[np + 1] == u) np++;
    if (l[u] != 0) {
      if (l[u] == 1 && np == n)
        l[u] = p;
      else
        ok = 0;
    } else {
      cnt++;
      l[u] = p, r[u] = np;
    }
    p = np + 1;
  }
  if (!ok)
    printf("0\n");
  else {
    int t = getLength(l[1], r[1], n);
    if (t == n)
      printf("%lld\n", getF(n));
    else {
      long long ans = 1, blank = 0;
      for (int i = 1; i <= n; i++) {
        if (l[i] == 0) {
          if (blank == 0) {
            ans = 0;
            break;
          } else {
            ans = ans * blank % mod;
            blank--;
          }
        } else {
          cnt--;
          if (getLength(l[i], r[i], n) > t) {
            ans = 0;
            break;
          }
          int left = l[i] - 1, right = r[i] + 1;
          if (left < 1) left += n;
          if (right > n) right -= n;
          if (a[left] > i && a[right] > i)
            fix[i] = l[i];
          else if (a[left] < i && a[right] < i) {
            int minn = r[i] - t + 1, maxn = l[i];
            ans = ans * (long long)getLength(minn, maxn, n);
            blank += getLength(fix[a[left]] + 1, r[i], n);
          } else if (a[left] < i) {
            fix[i] = r[i] - t + 1;
            if (fix[i] < 1) fix[i] += n;
            blank += getLength(fix[a[left]] + 1, fix[i] - 1, n);
          } else {
            fix[i] = l[i];
            blank += getLength(fix[i] + 1, fix[a[right]] - 1, n);
          }
          if (cnt == 0) {
            ans = ans * getF(n - i) % mod;
            break;
          }
        }
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
