#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const long long MOD = 998244353;
const int MAXN = 3e5 + 5;
int v[MAXN];
long long f[MAXN];
int l[MAXN], r[MAXN];
int coord[MAXN];
void solve() {
  f[0] = 1;
  for (int i = (1); i < (int)(MAXN); i++) {
    f[i] = (f[i - 1] * i) % MOD;
  }
  int n;
  scanf("%d", &n);
  for (int i = (0); i < (int)(n); i++) {
    scanf("%d", &v[i]);
  }
  if (v[0] == 1 && v[n - 1] == 1) {
    int s = n - 1;
    for (; s >= 0 && v[s] == 1; s--)
      ;
    if (s < 0) {
      printf("%lld\n", f[n]);
      return;
    }
    rotate(v, v + s + 1, v + n);
  }
  int s = 0;
  for (; s < n && v[s] != 1; s++)
    ;
  if (s == n) {
    printf("0\n");
    return;
  }
  rotate(v, v + s, v + n);
  int k = 0;
  for (k = 0; k < n && v[k] == 1; k++)
    ;
  l[1] = 0;
  r[1] = k - 1;
  for (int i = (k); i <= (int)(n); i++) {
    if (v[i] != v[i - 1]) {
      r[v[i - 1]] = i - 1;
      if (i != n) {
        if (v[i] == 1 || l[v[i]]) {
          printf("0\n");
          return;
        }
        l[v[i]] = i;
      }
    }
  }
  long long ans = 1ll;
  int vv = 0;
  for (int i = (2); i <= (int)(n); i++) {
    if (!l[i]) {
      if (vv == 0) {
        printf("0\n");
        return;
      }
      ans = (ans * vv) % MOD;
      vv--;
      continue;
    }
    if (r[i] - l[i] > k) {
      printf("0\n");
      return;
    }
    int lpos = -1, rpos = -1;
    int prv_i = v[l[i] - 1], nxt_i = v[(r[i] + 1) % n];
    if (prv_i > i) {
      if (nxt_i > i && r[i] - l[i] < k) {
        printf("0\n");
        return;
      }
      lpos = l[i];
      rpos = l[i];
      coord[i] = lpos;
      if (nxt_i < i) {
        vv += r[i] - l[i];
      }
    } else if (nxt_i > i) {
      lpos = r[i] - k + 1;
      rpos = r[i] - k + 1;
      coord[i] = lpos;
      vv += r[i] - l[i];
    } else {
      lpos = max(r[i] - k + 1, coord[prv_i] + 1);
      rpos = min(l[i], (coord[nxt_i] - 1 + n) % n);
      vv += (coord[nxt_i] + n - coord[prv_i] - 2) % n;
    }
    ans = (ans * (rpos - lpos + 1)) % MOD;
  }
  printf("%lld\n", ans);
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
