#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T>
void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
const int MOD = 998244353;
int N, a[100005 * 2], A[100005], st[100005][20], val[100005], len[100005],
    must[100005], sum[100005], ql, qr, cnt[100005];
bool vis[100005];
int inc(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int mul(int a, int b) { return 1LL * a * b % MOD; }
int Query(int l, int r) {
  int t = len[r - l + 1];
  return max(st[l][t], st[r - (1 << t) + 1][t]);
}
void Move(int l, int r) {
  while (qr < r) sum[val[++qr]]++;
  while (ql < l) sum[val[ql++]]--;
}
void Solve() {
  read(N);
  for (int i = 1; i <= N; ++i) {
    read(a[i]);
    a[i + N] = a[i];
  }
  int s = 0;
  for (int i = 2; i <= 2 * N; ++i) {
    if (a[i] == 1 && a[i - 1] != 1) {
      s = i;
      break;
    }
  }
  if (!s) {
    puts("0");
    return;
  }
  for (int i = 1; i <= N; ++i) A[i] = a[s++];
  int p = 1;
  while (A[p] == 1) ++p;
  --p;
  if (p == N) {
    int res = 1;
    for (int i = 1; i <= N; ++i) res = mul(res, i);
    out(res);
    putchar('\n');
    return;
  }
  int l = 0;
  for (int i = 1; i <= N; ++i) {
    if (A[i] != A[i - 1]) {
      if (vis[A[i]]) {
        puts("0");
        return;
      }
      vis[A[i]] = 1;
      l = 0;
    }
    ++l;
    if (l > p) {
      puts("0");
      return;
    }
  }
  for (int i = 1; i <= N; ++i) st[i][0] = A[i];
  for (int j = 1; j <= 18; ++j) {
    for (int i = 1; i <= N; ++i) {
      if (i + (1 << j) - 1 > N) break;
      st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
    }
  }
  for (int i = 2; i <= N; ++i) len[i] = len[i / 2] + 1;
  for (int i = 1; i <= N; ++i) {
    int r = min(i + p - 1, N);
    val[i] = Query(i, r);
  }
  int t = 0;
  ql = 1, qr = 0;
  for (int i = 1; i <= N; ++i) {
    if (A[i] != A[i - 1]) {
      if (t) {
        s = max(1, i - p);
        Move(s, t);
        must[A[i - 1]] = sum[A[i - 1]];
        if (!sum[A[i - 1]]) {
          puts("0");
          return;
        }
      }
      t = i;
    }
  }
  s = max(1, N - p + 1);
  Move(s, t);
  must[A[N]] = sum[A[N]];
  if (!sum[A[N]]) {
    puts("0");
    return;
  }
  for (int i = 1; i <= N; ++i) cnt[val[i]]++;
  for (int i = 1; i <= N; ++i) cnt[i] += cnt[i - 1];
  int ans = 1;
  for (int i = 1; i <= N; ++i) {
    if (must[i]) {
      ans = mul(ans, must[i]);
    } else
      ans = mul(ans, cnt[i] - (i - 1));
  }
  out(ans);
  putchar('\n');
}
int main() {
  Solve();
  return 0;
}
