#include <bits/stdc++.h>
using namespace std;
int n, A[600010], L[600010], R[600010], Fac[600010], QL[600010], QR[600010],
    Nw[600010];
int isq2[600010];
vector<int> arrg;
void GG() {
  puts("0");
  exit(0);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) Fac[i] = !i ? 1 : Fac[i - 1] * i % 998244353;
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  int fir1 = 1;
  while (fir1 <= n && A[fir1] == 1) fir1++;
  if (fir1 > n)
    fir1 = 1;
  else {
    int ff = fir1;
    while (fir1 <= n && A[fir1] != 1) fir1++;
    if (fir1 > n) {
      if (ff == 1)
        GG();
      else
        fir1 = 1;
    }
  }
  for (int i = 1; i < fir1; i++) A[i + n] = A[i];
  for (int i = 1; i <= n; i++) A[i] = A[i + fir1 - 1];
  for (int i = 1; i <= n; i++) {
    if (A[i] != A[i - 1]) {
      if (L[A[i]]) GG();
      L[A[i]] = i;
      R[A[i - 1]] = i - 1;
    }
  }
  R[A[n]] = n;
  arrg.clear();
  arrg.push_back(R[1]);
  A[n + 1] = 1;
  QL[1] = (QR[1] = R[1]);
  for (int i = 2; i <= n; i++) {
    if (!L[i]) {
      isq2[(int)arrg.size() - 1]++;
      continue;
    }
    if (R[i] - L[i] > R[1] - L[1]) GG();
    bool b1 = A[L[i] - 1] < i, b2 = A[R[i] + 1] < i;
    if (!b1 && !b2) {
      if (R[i] - L[i] != R[1] - L[1]) GG();
      QL[i] = (QR[i] = R[i]);
    } else if (!b1 && b2) {
      QL[i] = (QR[i] = L[i] + R[1] - L[1]);
    } else if (b1 && !b2) {
      QL[i] = (QR[i] = R[i]);
    } else {
      QL[i] = R[i];
      QR[i] = L[i] + R[1] - L[1];
    }
    for (int j = (b1 ? L[i] : L[i] + R[1] - L[1]);
         j <= (b2 ? R[i] + R[1] - L[1] : R[i]); j++)
      arrg.push_back((j - 1) % n + 1);
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++)
    if (QL[i]) {
      int nw = 0;
      for (int j = QL[i]; j <= QR[i]; j++)
        if (!Nw[(j - 1) % n + 1]) nw++;
      if (!nw) GG();
      ans = (long long)ans * nw % 998244353;
      for (int j = QL[i]; j <= QR[i]; j++)
        if (!Nw[(j - 1) % n + 1]) {
          Nw[(j - 1) % n + 1] = 1;
          break;
        }
    }
  assert(arrg.size() == n);
  int nw = 0;
  for (int i = 0; i < n; i++) {
    if (!Nw[arrg[i]]) nw++;
    while (isq2[i]--) ans = ((long long)ans * nw--) % 998244353;
  }
  printf("%lld\n", (ans % 998244353 + 998244353) % 998244353);
  return 0;
}
