#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 998244353
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 300005;

template <uint mod>
class ModInt {
private:
    uint v;
    static uint norm(const uint& x){ return x < mod ? x : x - mod; }
	static ModInt make(const uint& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static uint inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
	explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    uint operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        uint v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

using mod = ModInt<MOD>;

mod inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i=2;i<MAX_N;i++){
		inv[i] = MOD - inv[MOD % i] * (MOD / i);
		fac[i] = fac[i-1] * i;
		finv[i] = finv[i-1] * inv[i];
	}
}

mod comb(int a, int b)
{
	if(a<b) return 0;
	return fac[a] * finv[b] * finv[a-b];
}

int a[MAX_N], N;
bool used[MAX_N];

template<typename T> class segtree {
private:
    int n,sz;
    vector<T> node, min_val, second_val, lazy;
    vector<int> count;
    vector<bool> lazyFlag;
    void update(int id){
        node[id] = node[2*id+1] + node[2*id+2];
        if(min_val[2*id+1] < min_val[2*id+2]){
            min_val[id] = min_val[2*id+1];
            second_val[id] = min(second_val[2*id+1],min_val[2*id+2]);
            count[id] = count[2*id+1];
        }else if(min_val[2*id+1] > min_val[2*id+2]){
            min_val[id] = min_val[2*id+2];
            second_val[id] = min(min_val[2*id+1],second_val[2*id+2]);
            count[id] = count[2*id+2];
        }else{
            min_val[id] = min_val[2*id+1];
            second_val[id] = min(second_val[2*id+1],second_val[2*id+2]);
            count[id] = count[2*id+1]+count[2*id+2];
        }
    }
public:
    segtree(const vector<T>& v) {
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1,false);
        min_val.resize(2*n-1,numeric_limits<T>::max());
        second_val.resize(2*n-1,numeric_limits<T>::max());
        count.resize(2*n-1,1);
        rep(i,sz){
            node[i+n-1] = min_val[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            update(i);
        }
    }
    void eval(int k, int l, int r) {
        if(lazyFlag[k]){
            node[k] += (lazy[k] - min_val[k]) * count[k];
                min_val[k] = lazy[k];
                if(r - l > 1){
                    lazy[k*2+1] = lazy[k*2+2] = lazy[k];
                    lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
                }
            lazyFlag[k] = false;
        }
    }
    void _range(int a, int b, T x, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a || min_val[k] >= x){
            return;
        }
        if(a <= l && r <= b && second_val[k] > x) {
            lazy[k] = x;
            lazyFlag[k] = true;
            eval(k, l, r);
        }else{
            _range(a, b, x, 2*k+1, l, (l+r)/2);
            _range(a, b, x, 2*k+2, (l+r)/2, r);
            update(k);
        }
    }
    void range(int a, int b, T x){
        if(a < 0){
            _range(N+a, N, x);
            _range(0, b, x);
        }else{
            _range(a, b, x);
        }
    }
    T query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return 0;
        }
        if(a <= l && r <= b){
            return node[k];
        }
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};

int up[MAX_N];
int b[MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    N = n;
    rep(i,n){
        cin >> a[i];
    }
    int keta = 0;
    rep(i,n){
        if(a[i] == 1){
            int j = i;
            for(;j<i+n;++j,++keta){
                if(a[j%n] != 1) break;
            }
            j = i-1;
            for(;j>=i-n;--j, ++keta){
                if(a[(j+n)%n] != 1) break;
            }
            cmn(keta, n);
            break;
        }
    }
    make();
    if(keta == n){
        cout << fac[n] << "\n";
        return 0;
    }
    int st = n-1;
    for(;st>0;--st){
        if(a[st] != a[0]){
            st = (st+1)%n;
            break;
        }
    }
    rep(i,n){
        b[i] = a[i];
    }
    rep(i,n){
        a[i] = b[(i+st)%n];
    }
    // show(st);
    // show(keta);
    vp vec;
    P nw = P(1, 0);
    used[a[0]] = true;
    segtree<int> sg(vi(n, -INF));
    rep(i,n-1){
        if(a[i] == a[i+1]){
            ++nw.fi;
        }else{
            vec.pb(nw);
            if(used[a[i+1]] || nw.fi > keta){
                cout << "0\n";
                return 0;
            }else{
                used[a[i+1]] = true;
            }
            nw = P(1, i+1);
        }
    }
    vec.pb(nw);
    if(nw.fi > keta){
        cout << "0\n";
        return 0;
    }
    // sar(a, n);
    // show(vec);
    int m = len(vec);
    mod ans = 1;
    for(int i = 0; i < m; ++i){
        if(a[vec[i].se] > a[vec[(i+1)%m].se]){
            if(a[vec[(i+m-1)%m].se] > a[vec[i].se]){
                // a>b>c
                sg.range(vec[i].se, vec[i].se+1, INF);
                sg.range(vec[i].se+1, vec[i].se+vec[i].fi, a[vec[i].se]);
            }else{
                // a<b>c
                ans *= keta-vec[i].fi+1;
                sg.range(vec[i].se, vec[i].se+1, INF);
                int pos = vec[i].se-(keta-1);
                if(a[vec[(i+m-2)%m].se] > a[vec[(i+m-1)%m].se]){
                    cmx(pos, (vec[(i+m-1)%m].se+1>vec[i].fi)?(vec[(i+m-1)%m].se+1-n):(vec[(i+m-1)%m].se+1));
                }else{
                    cmx(pos, (vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1>vec[i].fi)?(vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1-n):(vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1));
                }
                sg.range(pos, vec[i].se+vec[i].fi, a[vec[i].se]);
            }
        }else{
            if(a[vec[(i+m-1)%m].se] > a[vec[i].se]){
                // a>b<c
                if(vec[i].fi != keta){
                    cout << "0\n";
                    return 0;
                }
                sg.range(vec[i].se, vec[i].se+1, INF);
                // cout << vec[i].se+1 << " " << vec[i].se+vec[i].fi << " " << a[vec[i].se] << "\n";
                sg.range(vec[i].se+1, vec[i].se+vec[i].fi, a[vec[i].se]);
            }else{
                // a<b<c
                sg.range((vec[i].se+vec[i].fi-keta+n)%n, (vec[i].se+vec[i].fi-keta+n)%n+1, INF);
                int pos = vec[i].se-(keta-1);
                if(a[vec[(i+m-2)%m].se] > a[vec[(i+m-1)%m].se]){
                    cmx(pos, (vec[(i+m-1)%m].se+1>vec[i].fi)?(vec[(i+m-1)%m].se+1-n):(vec[(i+m-1)%m].se+1));
                }else{
                    cmx(pos, (vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1>vec[i].fi)?(vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1-n):(vec[(i+m-1)%m].se+vec[(i+m-1)%m].fi-keta+1));
                }
                sg.range(pos, vec[i].se+vec[i].fi, a[vec[i].se]);
            }
        }
        // sg.print();
    }
    rep(i,n){
        int res = sg.query(i, i+1);
        if(res != INF)
            up[res]++;
    }
    // sar(up, n);
    int now = 0;
    for(int i = n; i >= 1; i--){
        if(!used[i]) now++;
        if(up[i] > now){
            cout << "0\n";
            return 0;
        }else{
            ans *= fac[now]*finv[now-up[i]];
            now -= up[i];
        }
    }
    cout << ans*fac[now] << "\n";
    return 0;
}
