#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <typename T>
void chmax(T &x, const T &y) {
  if (x < y) x = y;
}
int read() {
  char c;
  while ((c = getchar()) < '-')
    ;
  if (c == '-') {
    int x = (c = getchar()) - '0';
    while ((c = getchar()) >= '0') x = x * 10 + c - '0';
    return -x;
  }
  int x = c - '0';
  while ((c = getchar()) >= '0') x = x * 10 + c - '0';
  return x;
}
const int N = 3e5 + 5, D = 998244353;
int a0[N], a[N], mx_suf_k[N], pre_da[N], suf_da[N];
long long jie[N];
int cnt[N];
bool ap[N];
int main() {
  int n = read();
  int c1 = 0, p1 = 0;
  for (int i = 1; i <= n; ++i) {
    a0[i] = read();
    if (a0[i] == 1) {
      ++c1;
      p1 = i;
    }
  }
  jie[0] = 1;
  for (int i = 1; i <= n; ++i) jie[i] = jie[i - 1] * i % D;
  if (!c1) {
    puts("0");
    exit(0);
  }
  if (c1 == n) {
    cout << jie[n];
    exit(0);
  }
  while (a0[p1] == 1) p1 = (p1 == 1) ? n : p1 - 1;
  for (int i = 1; i <= n; ++i) a[i] = a0[p1 + i - (p1 + i > n ? n : 0)];
  for (int i = 1; i <= n; ++i) ap[a[i]] = 1;
  int k = c1;
  deque<int> q;
  for (int i = n; i >= 1; --i) {
    while (!q.empty() && a[i] > a[q.back()]) q.pop_back();
    q.push_back(i);
    while (q.front() - i >= k) q.pop_front();
    mx_suf_k[i] = a[q.front()];
    ++cnt[mx_suf_k[i]];
  }
  for (int i = 1; i <= n; ++i) {
    int j = i - 1;
    while (j && a[j] <= a[i]) j = pre_da[j];
    pre_da[i] = j;
  }
  for (int i = n; i >= 1; --i) {
    int j = i + 1;
    while (j <= n && a[j] <= a[i]) j = suf_da[j];
    suf_da[i] = j;
  }
  long long ans = 1;
  for (int i = 1; i <= n;) {
    int j = i;
    while (a[j] == a[i]) ++j;
    int l = pre_da[i], r = suf_da[i];
    if (r <= n) r -= k;
    r = min(i, r);
    ++l;
    l = max(l, j - k);
    if (l > r) {
      puts("0");
      exit(0);
    }
    ans = ans * (r - l + 1) % D;
    i = j;
  }
  int can = 0;
  for (int i = 1; i <= n; ++i) {
    can += cnt[i] - ap[i];
    if (!ap[i]) ans = ans * (can--) % D;
  }
  cout << ans;
}
