#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 998244353;
const ll INF = 1e18;
const int MX = 300001;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace io {
    // TYPE ID (StackOverflow)
    
    template<class T> struct like_array : is_array<T>{};
    template<class T, size_t N> struct like_array<array<T,N>> : true_type{};
    template<class T> struct like_array<vector<T>> : true_type{};
    template<class T> bool is_like_array(const T& a) { return like_array<T>::value; }

    // I/O 
    
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0);
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
    }
    
    // INPUT 
    
    template<class T> void re(T& x) { cin >> x; }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest);
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);
    
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); }
    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
    
    // OUTPUT 
    
    template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) {
        os << '{' << a.f << ", " << a.s << '}'; return os;
    }
    template<class T> ostream& printArray(ostream& os, const T& a, int SZ) {
        os << '{';
        F0R(i,SZ) {
            if (i) {
                os << ", ";
                if (is_like_array(a[i])) cout << "\n";
            }
            os << a[i];
        }
        os << '}';
        return os;
    }
    template<class T, size_t SZ> ostream& operator<<(ostream& os, const array<T,SZ>& a) {
        return printArray(os,a,SZ);
    }
    template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
        return printArray(os,a,sz(a));
    }
    template<class T> ostream& operator<<(ostream& os, const set<T>& a) {
        os << vector<T>(all(a)); return os;
    }
    template<class T1, class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {
        os << vector<pair<T1,T2>>(all(a)); return os;
    }
    
    template<class T> void pr(const T& x) { cout << x << '\n'; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { 
        cout << first << ' '; pr(rest...); 
    }
}

using namespace io;

namespace modOp {
    int ad(int a, int b, int mod = MOD) { return (a+b)%mod; }
    int sub(int a, int b, int mod = MOD) { return (a-b+mod)%mod; }
    int mul(int a, int b, int mod = MOD) { return (ll)a*b%mod; }
    
    int AD(int& a, int b, int mod = MOD) { return a = ad(a,b,mod); }
    int SUB(int& a, int b, int mod = MOD) { return a = sub(a,b,mod); }
    int MUL(int& a, int b, int mod = MOD) { return a = mul(a,b,mod); }
    
    int po (int b, int p, int mod = MOD) { return !p?1:mul(po(mul(b,b,mod),p/2,mod),p&1?b:1,mod); }
    int inv (int b, int mod = MOD) { return po(b,mod-2,mod); }
    
    int invGeneral(int a, int b) { // 0 < a < b, gcd(a,b) = 1
        if (a == 0) return b == 1 ? 0 : -1;
        int x = invGeneral(b%a,a); 
        return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
    }
}

using namespace modOp;

int N, numOp, totAvail, ans = 1;
set<pi> interval;
vi oc[MX];

pi getInt(vi v) {
    sort(all(v));
    if (sz(v) == 0) return {-1,-1};
    if (sz(v) == N) return {0,N-1};
    F0R(i,sz(v)) if ((v[i]+1)%N != (v[(i+1)%sz(v)])) {
        rotate(v.begin(),v.begin()+((i+1)%sz(v)),v.end());
        break;
    }
    F0R(i,sz(v)-1) if ((v[i]+1)%N != v[i+1]) {
        pr(0);
        exit(0);
    }
    return {v[0],v[0]+sz(v)-1};
}

int getLen(pi cur) {
    return cur.s-cur.f+1-numOp+1;
}

pi get(int x) {
    x %= N;
    auto it = interval.lb({x,MOD});
    if (it != interval.begin() && prev(it)->s >= x) {
        it = prev(it);
        auto a = *it; totAvail -= getLen(a); interval.erase(it);
        return a;
    }
    it = interval.lb({x+N,MOD});
    if (it != interval.begin() && prev(it)->s >= x+N) {
        it = prev(it);
        auto a = *it; totAvail -= getLen(a); interval.erase(it);
        return a;
    }
    return {-1,-1};
}

pi merge(pi a, pi b) {
    while (b.f < a.f) b.f += N, b.s += N;
    if (a.s+1 >= b.f) return {a.f,b.s};
    swap(a,b);
    while (b.f < a.f) b.f += N, b.s += N;
    if (a.s+1 >= b.f) {
        pi des = {a.f,b.s};
        while (des.f >= N) des.f -= N, des.s -= N;
        return des;
    }
    cout << "HUH " << a << " " << b;
    exit(0);
}

void ins(int x) {
    // circular 
    if (totAvail == N) return;
    while (x < 0) x += N;
    while (x >= N) x -= N;
    pi cur = {x,x+numOp-1};
    while (sz(interval)) {
        pi t = get(cur.f-1);
        if (t.f != -1) {
            cur = merge(cur,t);
            continue;
        }
        t = get(cur.s+1);
        if (t.f != -1) {
            cur = merge(cur,t);
            continue;
        }
        break;
    }
    if (cur.s-cur.f+1 >= N) {
        totAvail = N;
        return;
    }
    totAvail += getLen(cur);
    interval.insert(cur);
}

bool contain(int x) {
    x %= N;
    auto it = interval.lb({x,MOD});
    if (it != interval.begin() && prev(it)->s >= x) return 1;
    it = interval.lb({x+N,MOD});
    if (it != interval.begin() && prev(it)->s >= x+N) return 1;
    return 0;
}

int main() {
    // you should actually read the stuff at the bottom
    setIO(); re(N);
    F0R(i,N) {
        int x; re(x);
        oc[x].pb(i);
    }
    numOp = sz(oc[1]);
    if (numOp == N) {
        int ans = 1; FOR(i,1,N+1) MUL(ans,i);
        pr(ans);
        exit(0);
    }
    FOR(i,2,N+1) if (sz(oc[i]) > sz(oc[1])) {
        pr(0);
        exit(0);
    }
    FOR(i,1,N+1) {
        pi p = getInt(oc[i]);
        if (p.f == -1) {
            MUL(ans,totAvail+1-i);
            // pr(i,ans,interval,totAvail);
            continue;
        } 
        int pos = MOD;
        if (!contain(p.f-1)) pos = p.f;
        if (!contain(p.s+1)) {
            int t = p.s-numOp+1;
            if (pos != MOD && pos != t) {
                pr(0);
                exit(0);
            }
            pos = t;
        }
        //pr("AH",i,pos,interval,p.s+1,contain(p.s+1));
        if (pos == MOD) {
            int l = p.s-numOp+1, r = p.f;
            MUL(ans,r-l+1);
            ins(l);
        } else ins(pos);
        //pr(i,ans,interval,totAvail);
    }
    pr(ans);
    // you should actually read the stuff at the bottom
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/


Measured memory_bytes: 20224