#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 998244353;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num){
    fact[0] = 1;
    fact[1] = 1;
    for(int i=2; i<=num; i++){
        fact[i] = fact[i-1] * i % MOD;
    }
}

void create_seq_inv_mod(int num){
    seq_inv[0] = 1;
    seq_inv[1] = 1;
    for(int i=2; i<=num; i++){
        seq_inv[i] = (MOD - MOD/i) * seq_inv[MOD%i] % MOD;
    }
}

void create_fact_inv_mod(int num){
    fact_inv[0] = 1;
    fact_inv[1] = 1;
    for(int i=2; i<=num; i++){
        fact_inv[i] = fact_inv[i-1] * seq_inv[i] % MOD;
    }
}

void create_mod_tables(int num){
    fact.resize(num+1);
    seq_inv.resize(num+1);
    fact_inv.resize(num+1);
    create_fact_mod(num);
    create_seq_inv_mod(num);
    create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k){
    return fact[n] * fact_inv[n-k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k){
    return fact[n] * fact_inv[n-k] % MOD;
}

int64_t power_mod(int64_t num, int64_t power){
    int64_t prod = 1;
    num %= MOD;
    while(power > 0){
        if(power&1) prod = prod * num % MOD;
        num = num * num % MOD;
        power >>= 1;
    }
    return prod;
}

int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){
    int64_t d = a;
    if(b != 0){
        d = extgcd(b, a%b, y, x);
        y -= (a/b) * x;
    }else{
        x = 1; y = 0;
    }
    return d;
}

int64_t inv_mod(int64_t a){
    int64_t x, y;
    extgcd(a, MOD, x, y);
    return (MOD + x%MOD) % MOD;
}

void fail(){
    cout << 0 << endl;
    exit(0);
}

int main(){
    int N, A[300000];
    cin >> N;
    for(int i=0; i<N; i++){
        cin >> A[i];
        A[i]--;
    }
    create_mod_tables(N);

    if(A[0] >= A[N-1]){
        for(int i=1; i<N; i++) if(A[i-1] > A[i]){
            rotate(A, A+i, A+N);
            break;
        }
    }
    
    int len = 0, L = 0;
    vector<int> last(N, N);
    for(int i=0; i<N; i++){
        if(last[A[i]] + 1 < i) fail();
        last[A[i]] = i;
        if(i == 0 || A[i-1] == A[i]){
            len++;
            L = max(L, len);
        }else{
            len = 1;
        }
    }
    if(L == N){
        cout << (A[0] == 0 ? fact[N] : 0) << endl;
        return 0;
    }

    int64_t ans = 1;
    vector<pair<int, int>> conds;
    int s = 0;
    for(int t=1; t<=N; t++){
        if(t == N || A[t-1] > A[t]){
            if(A[s] == A[t-1]){
                if(t-s > 1) conds.push_back({A[s], t-s-1});
                s = t;
                continue;
            }
            int len = 1, num = L, mx = A[t-1];
            bool first = true;
            for(int i=t-2; i>=s; i--){
                if(A[i] == A[i+1]){
                    len++;
                }else{
                    if(first){
                        mul(ans, L-len+1);
                        first = false;
                    }
                    if(len > 1){
                        conds.push_back({mx, num-1});
                        num = len;
                        mx = A[i+1];
                    }
                    len = 1;
                }
            }
            if(len > 1) conds.push_back({mx, num-1});
            if(len < L) fail();
            s = t;
        }
    }
    sort(conds.rbegin(), conds.rend());

    int pool = 0;
    vector<int> avails;
    for(int i=0; i<N; i++) if(last[i] == N) avails.push_back(i);

    for(auto& p : conds){
        int lim = p.first, len = p.second;
        while(avails.size() && avails.back() > lim){
            pool++;
            avails.pop_back();
        }
        if(pool >= len){
            mul(ans, perm_mod(pool, len));
            pool -= len;
        }else{
            fail();
        }
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 12400