#include <bits/stdc++.h>
const int DEBUG = 0;
using namespace std;
const long long int mod = 998244353;
template <class I, class BiOp>
class SegTree {
  int n;
  std::vector<I> dat;
  BiOp op;
  I e;

 public:
  SegTree(int n_, BiOp op, I e) : op(op), e(e) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    dat.resize(2 * n);
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = e;
    }
  }
  void update(int k, I v) {
    k += n - 1;
    dat[k] = v;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }
  I querySub(int a, int b) const {
    I left = e;
    I right = e;
    a += n - 1;
    b += n - 1;
    while (a < b) {
      if ((a & 1) == 0) {
        left = op(left, dat[a]);
      }
      if ((b & 1) == 0) {
        right = op(dat[b - 1], right);
      }
      a = a / 2;
      b = (b - 1) / 2;
    }
    return op(left, right);
  }
  I query(int a, int b) const { return querySub(a, b + 1); }
};
void fail(void) {
  cout << 0 << endl;
  exit(0);
}
const int inf = 1e8;
long long int brute(const vector<int> &a, int len) {
  int n = a.size();
  vector<int> p(n);
  for (int i = (int)(0); i < (int)(n); i++) p[i] = i + 1;
  long long int ans = 0;
  do {
    vector<int> res(n, inf);
    for (int i = (int)(0); i < (int)(n); i++) {
      for (int j = (int)(0); j < (int)(len); j++) {
        int idx = (i + j) % n;
        res[idx] = min(res[idx], p[i]);
      }
    }
    if (res == a) ans++;
  } while (next_permutation(p.begin(), p.end()));
  return ans;
}
long long int solve(const vector<int> &a, int len) {
  int n = a.size();
  vector<int> mi(n + 1, inf), ma(n + 1, -inf);
  for (int i = (int)(0); i < (int)(n); i++) {
    int v = a[i];
    mi[v] = min(mi[v], i);
    ma[v] = max(ma[v], i);
  }
  {
    long long int sum = 0;
    for (int i = (int)(1); i < (int)(n + 1); i++) {
      if (mi[i] <= ma[i]) sum += ma[i] - mi[i] + 1;
    }
    if (sum != n) return 0;
  }
  if (DEBUG) {
    for (int i = (int)(1); i < (int)(n + 1); i++) {
      if (mi[i] <= ma[i])
        cerr << i << ": [" << mi[i] << "," << ma[i] << "]" << endl;
    }
  }
  vector<int> lo(n + 1, -inf), hi(n + 1, inf);
  for (int i = (int)(1); i < (int)(n + 1); i++) {
    if (mi[i] > ma[i]) continue;
    lo[i] = ma[i] - len + 1;
    hi[i] = mi[i];
    if (lo[i] > hi[i]) return 0;
  }
  SegTree<int, plus<int> > fr(n, plus<int>(), 0);
  for (int i = (int)(0); i < (int)(n); i++) fr.update(i, 1);
  vector<int> fb(n, 0);
  int rem = n;
  long long int prod = 1;
  for (int i = n; i >= 1; --i) {
    assert(rem == fr.query(0, n - 1));
    long long int fac = 1;
    if (mi[i] > ma[i]) {
      fac = rem - i + 1;
      if (fac < 0) fac = 0;
    } else {
      int cnt = fr.query(lo[i], hi[i]);
      fac = cnt;
      for (int j = (int)(mi[i]); j < (int)(ma[i] + 1); j++) {
        if (fb[j] == 0) {
          fb[j] = 1;
          fr.update(j, 0);
          rem--;
        }
      }
      for (int j = (int)(1); j < (int)(len); j++) {
        int idx = (mi[i] - j + n) % n;
        if (fb[idx]) break;
        fb[idx] = 1;
        fr.update(idx, 0);
        rem--;
      }
    }
    if (i == 1) assert(fac == 1);
    prod *= fac;
    prod %= mod;
    if (DEBUG) {
      cerr << i << ": fb:";
      for (int j = (int)(0); j < (int)(n); j++) cerr << " " << fb[j];
      cerr << endl;
      cerr << "fac"
           << "=" << fac << "\n";
    }
  }
  assert(rem == 0);
  return prod;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = (int)(0); i < (int)(n); i++) cin >> a[i];
  int fst = 0;
  int pos = 0;
  while (pos < n && a[pos] == 1) pos++;
  int idx = -1;
  for (int i = (int)(pos); i < (int)(n); i++) {
    if (a[i] == 1) {
      idx = i;
      break;
    }
  }
  if (idx != -1) {
    fst = idx;
  } else {
    fst = pos == 0 ? -1 : 0;
  }
  if (fst < 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> b(n);
  for (int i = (int)(0); i < (int)(n); i++) {
    b[i] = a[(fst + i) % n];
  }
  if (DEBUG) {
    cerr << "b:";
    for (int i = (int)(0); i < (int)(n); i++) cerr << " " << b[i];
    cerr << endl;
  }
  int ma = 0;
  for (int i = (int)(0); i < (int)(n); i++)
    if (b[i] == 1) ma = i;
  int len = ma + 1;
  for (int i = (int)(0); i < (int)(len); i++) {
    if (b[i] != 1) {
      fail();
    }
  }
  if (len == n) {
    long long int prod = 1;
    for (int i = (int)(0); i < (int)(n); i++) prod = prod * (i + 1) % mod;
    cout << prod << endl;
    return 0;
  }
  long long int ans = solve(b, len);
  cout << ans << endl;
}
