#include <bits/stdc++.h>
using namespace std;
bool used[333333];
int mp[333333];
int main() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  set<long long> s;
  for (long long(i) = (0); (i) < (n); ++i) {
    cin >> v[i];
    s.insert(v[i]);
  }
  if (s.size() == 1) {
    cout << s.count(1) << endl;
    return 0;
  }
  deque<int> deq;
  deque<int> ldeq;
  long long now = -1, pla = 0, st = -1, ans = 1, malen = 0, nlen = 0;
  bool flag = true;
  vector<pair<pair<long long, long long>, pair<long long, long long>>> seica;
  while (1) {
    if (pla == st) break;
    if (v[pla] != now) {
      now = v[pla];
      deq.push_back(now);
      ldeq.push_back(nlen);
      if (deq.size() > 3) deq.pop_front();
      if (ldeq.size() > 3) ldeq.pop_front();
      if (deq.size() == 3) {
        if (flag) {
          st = pla;
          flag = false;
        }
        if (used[deq[1]]) {
          cout << 0 << endl;
          return 0;
        }
        seica.push_back(
            make_pair(make_pair(deq[1], ldeq[2]), make_pair(deq[0], deq[2])));
        used[deq[1]] = true;
        malen = max(malen, nlen);
      }
      nlen = 1;
    } else {
      nlen++;
    }
    pla = (pla + 1) % n;
  }
  sort(seica.begin(), seica.end());
  if (malen > seica[0].first.second) {
    cout << 0 << endl;
    return 0;
  }
  deque<pair<int, int>> p_deq;
  pla = 0;
  for (long long(i) = (0); (i) < (n); ++i) {
    while (pla - i < malen) {
      while (p_deq.size() && p_deq[p_deq.size() - 1].first < v[pla % n])
        p_deq.pop_back();
      p_deq.push_back(make_pair(v[pla % n], pla));
      pla++;
    }
    while (p_deq[0].second < i) p_deq.pop_front();
    mp[p_deq[0].first]++;
  }
  cout << "mp" << endl;
  for (long long(i) = (0); (i) < (n); ++i) cout << mp[i + 1] << " ";
  cout << endl;
  now = 0;
  pla = 0;
  for (long long i = 1; i <= n; i++) {
    pla += mp[i];
    if (pla == 0) {
      cout << 0 << endl;
      return 0;
    }
    cout << i << " " << pla << " " << endl;
    if (now < seica.size() && seica[now].first.first == i) {
      if (i > seica[now].second.first && i > seica[now].second.second) {
        ans *= malen - seica[now].first.second + 1;
        ans %= 998244353;
      }
      now++;
    } else {
      ans *= pla;
      ans %= 998244353;
    }
    pla--;
  }
  cout << ans << endl;
  return 0;
}
