#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  int size;
  vector<int> parent;
  UnionFind() {}
  UnionFind(int size) {
    this->size = size;
    parent.resize(size + 1);
    init();
  }
  void init() {
    for (int i = 0; i <= size; i++) parent[i] = i;
  }
  int root(int i) {
    if (parent[i] == i) return i;
    return parent[i] = root(parent[i]);
  }
  bool same(int i, int j) { return root(i) == root(j); }
  void unite(int i, int j) {
    int root_i = root(i), root_j = root(j);
    if (root_i == root_j) return;
    parent[root_i] = root_j;
  }
};
long long n;
long long a[300005];
long long l[300005], r[300005];
long long R[300005];
UnionFind uf(300005);
set<long long> S;
int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) uf.unite(i, i + 1);
  }
  if (a[0] == a[n - 1]) uf.unite(0, n - 1);
  long long len = 0;
  for (int i = 0; i < n; i++) {
    if (R[a[i]] != 0 && R[a[i]] != uf.root(i)) {
      cout << 0 << endl;
      return 0;
    }
    R[a[i]] = uf.root(i);
    if (a[i] == 1) len++;
  }
  if (len == n) {
    long long ans = 1;
    for (int i = 1; i <= n; i++) ans *= i, ans %= 998244353;
    cout << ans << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) l[i] = 1000000000, r[i] = -1000000000;
  for (long long i = 0; i < n; i++) {
    l[a[i]] = min(l[a[i]], i);
    r[a[i]] = max(r[a[i]], i);
  }
  if (a[0] == a[n - 1]) {
    for (int i = 0; i < n; i++) {
      if (a[i] != a[0]) {
        r[a[0]] = i - 1;
        break;
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (a[i] != a[n - 1]) {
        l[a[n - 1]] = i + 1;
        break;
      }
    }
    if (l[a[0]] > r[a[0]]) r[a[0]] += n;
  }
  for (int i = 1; i <= n; i++) {
    if (l[i] == 1000000000) continue;
    if (r[i] - l[i] + 1 > len) {
      cout << 0 << endl;
      return 0;
    }
  }
  long long ans = 1, valid = 0;
  for (int i = l[1]; i <= r[1]; i++) S.insert(i % n);
  for (int i = 2; i <= n; i++) {
    if (l[i] == 1000000000) {
      if (valid <= 0) {
        cout << 0 << endl;
        return 0;
      }
      ans *= valid, ans %= 998244353;
      valid--;
      continue;
    }
    bool lflag = S.count((l[i] - 1 + n) % n), rflag = S.count((r[i] + 1) % n);
    if (!lflag && !rflag) {
      cout << 0 << endl;
      return 0;
    }
    if (!lflag || !rflag)
      valid += r[i] - l[i];
    else {
      ans *= len - (r[i] - l[i] + 1) + 1, ans %= 998244353;
      valid += len - 1 + (r[i] - l[i] + 1) - 1;
    }
    for (int j = l[i]; j <= r[i]; j++) S.insert(j % n);
  }
  cout << ans << endl;
  return 0;
}
