#include <bits/stdc++.h>
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
using namespace std;
class Solve {
 public:
  using P = pair<int, int>;
  int N;
  vector<int> A;
  vector<P> seq;
  void input() {
    cin >> N;
    for (int i = int(0); i < int(N); i++) {
      int tmp;
      cin >> tmp;
      A.push_back(tmp);
    }
  }
  bool all1() {
    for (auto n : A) {
      if (n != 1) return false;
    }
    return true;
  }
  void count() {
    int num = -1;
    int cnt = 0;
    int pt = 0;
    while (true) {
      if (pt == N) {
        seq.emplace_back(num, cnt);
        break;
      }
      if (pt == 0) {
        num = A[pt];
        cnt = 1;
        pt++;
        continue;
      }
      if (A[pt] != num) {
        seq.emplace_back(num, cnt);
        num = A[pt];
        cnt = 1;
      } else {
        cnt++;
      }
      pt++;
    }
    if (seq.front().first == seq.back().first) {
      seq.front().second += seq.back().second;
      seq.pop_back();
    }
  }
  bool check() {
    set<int> st;
    for (auto e : seq) {
      if (st.count(e.first) == 0) {
        st.insert(e.first);
      } else {
        return false;
      }
    }
    if (st.count(1) == 0) {
      return false;
    }
    int chan = 0;
    int val = 0;
    for (int i = 0; i < seq.size(); ++i) {
      if (i == seq.size() - 1) {
        if ((seq[0].first - seq[i].first * val < 0)) {
          chan++;
        }
      } else {
        if (val == 0) {
          if (seq[i + 1].first - seq[i].first > 0) {
            val = 1;
          } else if (seq[i + 1].first - seq[i].first < 0) {
            val = -1;
          }
        } else if (val == 1) {
          if (seq[i + 1].first - seq[i].first < 0) {
            val = -1;
            chan++;
          }
        } else {
          if (seq[i + 1].first - seq[i].first > 0) {
            val = 1;
            chan++;
          }
        }
      }
    }
    if (chan > 2) return false;
    int one_val = -1;
    for (auto e : seq) {
      if (e.first == 1) one_val = e.second;
    }
    for (auto e : seq) {
      if (one_val < e.second) {
        return false;
      }
    }
    return true;
  }
  long long int solve2() {
    int center = -1;
    int last = -1;
    int max_v = -1;
    int m = seq.size();
    int cnt1 = -1;
    int cntmax = -1;
    for (int i = 0; i < m; ++i) {
      if (seq[i].first == 1) {
        center = i;
        cnt1 = seq[i].second;
      }
      if (max_v < seq[i].first) {
        max_v = seq[i].first;
        last = i;
        cntmax = seq[i].second;
      }
    }
    vector<P> L, R;
    if (center < last) {
      for (int i = center + 1; i < last; ++i) {
        R.push_back(seq[i]);
      }
      for (int i = center - 1; i >= 0; --i) {
        L.push_back(seq[i]);
      }
      for (int i = m - 1; i > last; --i) {
        L.push_back(seq[i]);
      }
    } else {
      for (int i = center + 1; i < m; ++i) {
        R.push_back(seq[i]);
      }
      for (int i = 0; i < last; ++i) {
        R.push_back(seq[i]);
      }
      for (int i = center - 1; i > last; --i) {
        L.push_back(seq[i]);
      }
    }
    long long int ans = 1ll;
    int rest = 0;
    vector<int> put(N);
    put[0] = 1;
    int pl = 0, pr = 0;
    int ql = cnt1, qr = N - 1;
    for (int i = 2; i <= N; ++i) {
      if (pl < L.size() && L[pl].first == i) {
        int inc = L[pl].second;
        int pt = qr - inc + 1;
        if (put[pt]) return 0;
        put[pt] = 1;
        rest += inc - 1;
        qr -= inc;
        pl++;
      } else if (pr < R.size() && R[pr].first == i) {
        int inc = R[pr].second;
        int pt = ql + inc - cnt1;
        if (put[pt]) return 0;
        put[pt] = 1;
        ql += inc;
        rest += inc - 1;
        pr++;
      } else {
        if (i < max_v) {
          ans *= rest;
          ans %= 998244353;
          rest--;
        } else if (i == max_v) {
          int start = qr - cnt1 + 1;
          int end = ql;
          int kouho = 0;
          if (start >= 0) {
            for (int k = start; k <= end; ++k) {
              if (!put[k]) kouho++;
            }
          } else {
            for (int k = 0; k <= end; ++k) {
              if (!put[k]) kouho++;
            }
            for (int k = start + N; k <= end; ++k) {
              if (!put[k]) kouho++;
            }
          }
          ans *= kouho;
          ans %= 998244353;
          rest = N - i;
        } else {
          ans *= rest;
          ans %= 998244353;
          rest--;
        }
      }
    }
    return ans;
  }
  void solve() {
    input();
    if (all1()) {
      long long int ans = 1ll;
      for (long long int i = 1; i <= N; ++i) {
        ans *= i;
        ans %= 998244353;
      }
      cout << ans << endl;
      return;
    }
    count();
    if (!check()) {
      cout << 0 << endl;
      return;
    }
    cout << solve2() << endl;
    int a = 3 + 4;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Solve().solve();
  return 0;
}
