#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}
const int N = 300005, mod = 998244353;
int n, a[N], b[N];
struct Seg {
  int L, R, v;
} s[N];
int rs[N], rl[N];
void push(int v, int L, int R) {
  if (rs[v]) {
    puts("0");
    exit(0);
  }
  rs[v] = L, rl[v] = R - L + 1;
}
int Max[N << 2];
void update(int rt, int L, int R, int xL, int xR, int d) {
  if (R < xL || L > xR) return;
  if (L <= xL && xR <= R) {
    Max[rt] = max(Max[rt], d);
    return;
  }
  int mid = (L + R) >> 1, ls = rt << 1, rs = ls | 1;
  update(ls, L, mid, xL, xR, d);
  update(rs, mid + 1, R, xL, xR, d);
}
int query(int rt, int L, int R, int x) {
  if (L == R) return Max[rt];
  int mid = (L + R) >> 1, ls = rt << 1, rs = ls | 1;
  if (x <= mid)
    return max(Max[rt], query(ls, L, mid, x));
  else
    return max(Max[rt], query(rs, mid + 1, R, x));
}
vector<int> res;
int main() {
  n = read();
  for (int i = 0; i < n; i++) a[i] = read();
  int k = 0;
  while (k < n && a[k] != 1) k++;
  if (k >= n) return puts("0"), 0;
  if (k == 0) {
    if (a[n - 1] == 1) {
      k = n - 1;
      while (k >= 0 && a[k] == 1) k--;
    }
    if (k < 0) {
      int ans = 1;
      for (int i = 1; i <= n; i++) ans = 1LL * ans * i % mod;
      return printf("%d", ans), 0;
    }
  }
  for (int i = 0; i < n; i++) b[(i + k) % n] = a[i];
  for (int i = 0; i < n; i++) a[i] = b[i];
  int t = 0;
  while (a[t] == 1) t++;
  int m = 0;
  for (int i = 0, j;; i = j) {
    for (j = i; a[i] == a[j]; j = (j + 1) % n)
      ;
    s[m].L = i, s[m].R = (j + n - 1) % n, s[m].v = a[i];
    m++;
    if (j == 0) break;
  }
  for (int i = 0; i < m; i++)
    if (s[i].R - s[i].L + 1 != t) {
      if (s[i].R - s[i].L + 1 > t) return puts("0"), 0;
      if ((s[(i + m - 1) % m].v < s[i].v) ^ (s[i].v < s[(i + 1) % m].v))
        return puts("0"), 0;
    } else {
      if (rs[s[i].v]) return puts("0"), 0;
      push(s[i].v, s[i].L, s[i].R);
    }
  for (int i = 0, j;; i = j) {
    for (int j = i + 1; j < m && s[j].R - s[j].L + 1 != t; j++)
      ;
    if (j == i + 1) continue;
    if (s[i].v < s[i + 1].v) {
      for (int k = i + 1; k < j; k++) push(s[k].v, s[k].R - t + 1, s[k].R);
    } else {
      for (int k = i + 1; k < j; k++) push(s[k].v, s[k].L, s[k].R);
    }
  }
  for (int i = 1; i <= n; i++)
    if (rs[i]) update(1, 1, n, rs[i] + 1, rs[i] + rl[i], i);
  res.clear();
  for (int i = 1; i <= n; i++) res.push_back(query(1, 1, n, i));
  sort(res.begin(), res.end());
  reverse(res.begin(), res.end());
  res.push_back(n + 1);
  int T = 0, ans = 1;
  for (int i = 0; i < res.size() - 1; i++) {
    if (res[i] == res[i + 1]) {
      if (n - res[i] - T <= 0) return puts("0"), 0;
      ans = 1LL * ans * (n - res[i] - T) % mod;
    }
    T++;
  }
  cout << ans;
  return 0;
}
