#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = mod * mod;
const double eps = 1e-8;
const double pi = acos(-1.0);
struct SegT {
 private:
  int n;
  vector<long long> node;
  vector<bool> lazy;
  const long long init_c = 0;

 public:
  void init(vector<int> v) {
    n = 1;
    while (n < (int)v.size()) n <<= 1;
    node.resize(2 * n - 1, 0);
    for (int i = 0; i < v.size(); i++) {
      node[i + n - 1] = v[i];
    }
    for (int i = n - 1 - 1; i >= 0; i--) {
      node[i] = node[2 * i + 1] + node[2 * i + 2];
    }
    lazy.resize(2 * n - 1, 0);
  }
  long long f(long long a, long long b) { return a + b; }
  void eval(int k, int l, int r) {
    if (lazy[k]) {
      node[k] = 0;
      if (r - l > 1) {
        lazy[2 * k + 1] = true;
        lazy[2 * k + 2] = true;
      }
      lazy[k] = 0;
    }
  }
  void add(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      lazy[k] = true;
      eval(k, l, r);
    } else {
      add(a, b, k * 2 + 1, l, (l + r) / 2);
      add(a, b, k * 2 + 2, (l + r) / 2, r);
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return init_c;
    if (a <= l && r <= b)
      return node[k];
    else {
      long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  if (a[0] == 1) {
    for (int i = n - 1; i >= 0; i--) {
      if (a[i] > 1) {
        vector<int> b;
        for (int j = 0; j < n; j++) {
          b.push_back(a[(i + 1 + j) % n]);
        }
        a = b;
        break;
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (a[i] == 1) {
        vector<int> b;
        for (int j = 0; j < n; j++) {
          b.push_back(a[(i + j) % n]);
        }
        a = b;
        break;
      } else if (i == n - 1) {
        cout << 0 << endl;
        return;
      }
    }
  }
  int k = n;
  for (int i = 0; i < n; i++) {
    if (a[i] > 1) {
      k = i;
      break;
    }
  }
  if (k == n) {
    long long ans = 1;
    for (int i = 1; i <= n; i++) ans = ans * i % mod;
    cout << ans << endl;
    return;
  }
  if (k == 1) {
    sort(a.begin(), a.end());
    for (int i = 0; i < n; i++) {
      if (a[i] != i + 1) {
        cout << 0 << endl;
        return;
      }
    }
    cout << 1 << endl;
    return;
  }
  vector<int> v(n, 1);
  vector<bool> used(n + 1, false);
  vector<pair<int, int> > loc(n + 1);
  v[0] = 0;
  used[1] = true;
  int memo;
  for (int i = k; i < n; i++) {
    if (a[i - 1] > a[i]) {
      cout << 0 << endl;
      return;
    }
    used[a[i]] = true;
    int le = i;
    while (i + 1 < n && a[i] == a[i + 1]) i++;
    loc[a[i]] = {le, i};
    if (i - le + 1 > k) {
      cout << 0 << endl;
      return;
    }
    memo = i - le + 1;
    v[i - k + 1] = 0;
  }
  SegT st;
  st.init(v);
  vector<long long> rest(n + 1, 0);
  for (int i = n; i >= 1; i--) {
    if (used[i]) {
      int le = loc[i].first, ri = loc[i].second;
      int sle = le - k + 1;
      if (sle >= 0) {
        st.add(sle, ri + 1);
      } else {
        st.add(0, ri + 1);
        st.add(sle + n, n);
      }
    } else {
      rest[i] = st.query(0, n);
    }
  }
  long long ans = k - memo + 1;
  int tmp = 0;
  for (int i = 1; i <= n; i++) {
    if (used[i]) continue;
    long long num = rest[i] - tmp;
    if (num <= 0) {
      cout << 0 << endl;
      return;
    }
    ans = ans * num % mod;
    tmp++;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
