#include <bits/stdc++.h>
using namespace std;
static int n;
static int A[300000 * 2 + 10], l[300000 + 10], r[300000 + 10], c[300000 + 10];
static bool has[300000 + 10];
static vector<int> op[300000 + 10];
inline int nxt(int i) { return i < n ? i + 1 : 1; }
inline int pre(int i) { return i > 1 ? i - 1 : n; }
inline int pos(int i) { return i < 1 ? i + n : (i > n ? i - n : i); }
inline long long fac(int x) {
  long long r = 1;
  for (; x; x--) r = r * x % 998244353;
  return r;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", A + i);
  memcpy(A + n + 1, A + 1, sizeof(int) * n);
  if (all_of(A + 1, A + n + 1, [](int x) { return x == A[1]; }))
    printf("%lld\n",
           all_of(A + 1, A + n + 1, [](int x) { return x == 1; }) ? fac(n) : 0);
  else {
    int d = 0, s = 1;
    long long ans = 1, cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (i == 1 && A[1] == A[n]) {
        while (A[i + 1] == A[1]) i++;
        s = i + 1;
        continue;
      }
      if (l[A[i]]) ans = 0;
      l[A[i]] = i;
      has[A[i]] = true;
      for (r[A[i]] = i; A[r[A[i]] + 1] == A[i]; r[A[i]]++)
        ;
      if (A[i] == 1) d = r[A[i]] - l[A[i]] + 1;
      i = r[A[i]];
    }
    for (int i = 1; i <= n; i++) {
      if (!l[i]) continue;
      int pl = l[i] - d + 1;
      if (pl < 1) {
        op[1].push_back(i);
        op[r[i] + 1].push_back(-i);
        op[pos(pl)].push_back(i);
      } else if (r[i] > n) {
        op[1].push_back(i);
        op[pos(r[i]) + 1].push_back(-i);
        op[pl].push_back(i);
      } else {
        op[pl].push_back(i);
        op[r[i] + 1].push_back(-i);
      }
    }
    set<int> q;
    for (int i = 1; i <= n; i++) {
      for (int v : op[i]) {
        if (v > 0)
          q.insert(v);
        else
          q.erase(-v);
      }
      if (!q.empty()) c[*q.rbegin()]++;
    }
    for (int i = n; i >= 1; i--) {
      if (!has[i]) {
        cnt++;
        continue;
      }
      int len = r[i] - l[i] + 1;
      if (r[i] > n) r[i] -= n;
      if (len > d) ans = 0;
      if (A[pre(l[i])] < i && A[nxt(r[i])] < i)
        ans = ans * (d - len + 1) % 998244353;
      for (int j = 0; j < c[i] - 1; j++, cnt--) ans = ans * cnt % 998244353;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
