#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  if (f) x = -x;
}
void claim(int x) {
  if (!x) puts("0"), exit(0);
}
const int N = 300005, mod = 998244353;
int a[N], b[N], c[N], l[N], r[N], vis[N];
int n, p, len, ans = 1, rem;
int main() {
  read(n);
  for (int i = (1); i <= (n); i++) {
    read(a[i]);
    if (!p && a[i] == 1) p = i;
  }
  if (a[n] == 1) {
    p = n;
    while (a[p - 1] == 1) p--;
  }
  claim(p);
  for (int i = (1); i <= (p - 1); i++) b[i] = a[i];
  for (int i = (1); i <= (n - p + 1); i++) a[i] = a[i + p - 1];
  for (int i = (1); i <= (p - 1); i++) a[i + n - p + 1] = b[i];
  for (int i = (1); i <= (n); i++) {
    c[a[i]]++;
    if (!l[a[i]]) l[a[i]] = i;
    r[a[i]] = i;
  }
  for (int i = (1); i <= (n); i++)
    claim(!c[i] || c[i] == r[i] - l[i] + 1 && c[i] <= c[1]);
  len = c[1];
  for (int i = (2); i <= (n); i++) {
    if (!c[i]) {
      claim(rem);
      ans = (long long)ans * (rem--) % mod;
      continue;
    }
    int L = (a[l[i] - 1] < i), R = (a[r[i] + 1] < i);
    if (L && R) {
      ans = (long long)ans * (len - c[i] + 1) % mod;
      rem += c[i] + len - 2;
    } else if (L || R)
      rem += c[i] - 1;
    else
      claim(c[i] == len);
  }
  cout << ans << endl;
  return 0;
}
