#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int A[300300];
int N;
int cnt[300300];
vector<pair<int, int> > vec;
bool checkAll1() {
  for (int i = 0; i < N; ++i) {
    if (A[i] != 1) return false;
  }
  return true;
}
void input() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) scanf("%d", A + i);
}
vector<pair<int, int> > rot(vector<pair<int, int> > vec) {
  vector<pair<int, int> > res;
  int id = -1;
  for (int i = 0; i < vec.size(); ++i) {
    if (vec[i].first == 1) {
      id = i;
      break;
    }
  }
  if (id == -1) return res;
  for (int i = 0; i < vec.size(); ++i) {
    res.push_back(vec[(id + i) % vec.size()]);
  }
  return res;
}
bool check(vector<pair<int, int> > vec) {
  int c0 = vec[0].second;
  set<int> se;
  for (int i = 0; i < vec.size(); ++i) {
    if (vec[i].second > c0) return false;
    se.insert(vec[i].first);
  }
  if (se.size() != vec.size()) return false;
  return true;
}
int cand[300300];
bool decided[300300];
long long solve() {
  bool flg = checkAll1();
  if (flg) {
    long long ans = 1;
    for (int i = 1; i <= N; ++i) ans = (ans * i) % mod;
    return ans;
  }
  for (int i = 0; i < N; ++i) {
    if (i == 0)
      cnt[0] = 1;
    else {
      if (A[i - 1] == A[i])
        cnt[i] = cnt[i - 1] + 1;
      else
        cnt[i] = 1;
    }
  }
  for (int i = 0; i < N; ++i) {
    if (i == N - 1 || A[i + 1] != A[i]) {
      vec.push_back(pair<int, int>(A[i], cnt[i]));
    }
  }
  if (vec.size() > 1 && vec[0].first == vec.back().first) {
    vec[0].second += vec.back().second;
    vector<pair<int, int> > tmp;
    for (int i = 0; i + 1 < vec.size(); ++i) tmp.push_back(vec[i]);
    swap(vec, tmp);
  }
  vec = rot(vec);
  flg = check(vec);
  if (!flg) return 0;
  int c0 = vec[0].second;
  for (int i = 0; i < vec.size(); ++i) {
    int lb = vec[i].first;
    decided[lb] = true;
    if (i + 1 < vec.size()) lb = max(lb, vec[i + 1].first);
    int cnt;
    if (i + 1 < vec.size()) {
      cnt = vec[i + 1].second;
    } else {
      cnt = c0;
    }
    cnt--;
    cand[lb] += cnt;
  }
  return -1;
  int rem = 0;
  long long ans = 1;
  for (int i = 1; i <= N; ++i) {
    rem += cand[i];
    if (decided[i]) continue;
    if (rem <= 0) return 0;
    ans = (ans * rem) % mod;
    rem--;
  }
  for (int i = 1; i < vec.size(); ++i) {
    int cur = vec[i].first;
    int prv = vec[i - 1].first;
    int nxt = (i + 1 < vec.size()) ? (vec[i + 1].first) : (vec[0].first);
    if (cur > prv && cur > nxt) {
      int num = c0 - vec[i].second + 1;
      ans = (ans * num) % mod;
    }
    if (cur < prv && cur < nxt) {
      if (vec[i].second != c0) return 0;
    }
  }
  return ans;
}
int main() {
  input();
  long long ans = solve();
  printf("%lld\n", ans);
  return 0;
}
