You are given a sequence of N integers: A_1,A_2,...,A_N.

Find the number of permutations p_1,p_2,...,p_N of 1,2,...,N that can be changed to A_1,A_2,...,A_N by performing the following operation some number of times (possibly zero), modulo 998244353:

* For each 1\leq i\leq N, let q_i=min(p_{i-1},p_{i}), where p_0=p_N. Replace the sequence p with the sequence q.

Constraints

* 1 \leq N \leq 3 × 10^5
* 1 \leq A_i \leq N
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1
:
A_N


Output

Print the number of the sequences that satisfy the condition, modulo 998244353.

Examples

Input

3
1
2
1


Output

2


Input

5
3
1
4
1
5


Output

0


Input

8
4
4
4
1
1
1
2
2


Output

24


Input

6
1
1
6
2
2
2


Output

0
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

template<unsigned MOD_> struct ModInt {
    static const unsigned MOD = MOD_;
    unsigned x;
    void undef() { x = (unsigned)-1; }
    bool isnan() const { return x == (unsigned)-1; }
    inline int geti() const { return (int)x; }
    ModInt() { x = 0; }
    ModInt(const ModInt &y) { x = y.x; }
    ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; }
    ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; }
    ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; }
    ModInt operator-() const { return (x ? MOD-x: 0); }

    ModInt inv() const { return pow(MOD-2); }
    ModInt pow(long long y) const {
	ModInt b = *this, r = 1;
	if (y < 0) { b = b.inv(); y = -y; }
	for (; y; y>>=1) {
	    if (y&1) r *= b;
	    b *= b;
	}
	return r;
    }

    ModInt extgcd() const {
	unsigned a = MOD, b = x; int u = 0, v = 1;
	while (b) {
	    int t = a / b;
	    a -= t * b; swap(a, b);
	    u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return ModInt(u);
    }

    friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
    friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
    friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
    friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
    friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
    friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
    friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};

const LL MOD = 998244353;
typedef ModInt<MOD> Mint;

Mint fact(int n) {
    Mint r = 1;
    REP (i, n) r *= i+1;
    return r;
}

int N;
int A[300011];
int B[300011];
int buf[300011];
int L[300011], R[300011];
int C[300011];
int POS[300011];
int put[300011];
int step;

Mint calc() {
    int prv = A[N-1];
    REP (i, N) {
	if (prv != A[i]) {
	    if (L[A[i]]) return 0;
	    POS[A[i]] = i;
	    L[A[i]] = prv;
	    R[prv] = A[i];
	}
	prv = A[i];
    }

    Mint ans = 1;
    B[N] = 1;
    int fr = 0;
    for (int i=1; i<=N; i++) {
	//printf("# %d %d (%d %d)\n", i, fr, L[i], R[i]);
	const int pos = POS[i];
	if (C[i] == 0) {
	    ans *= fr;
	    fr--;
	} else if (L[i] > i) {
	    put[i] = pos;
	    REP (t, step) {
		if (B[pos+t]) {
		    if (t != C[i]) return 0;
		    break;
		} else if (t > C[i]) {
		    return 0;
		}
		B[pos+t] = i;
	    }
	    if (R[i] < i) fr += (R[i]!=1? put[R[i]]: N) - put[i] - 1;
	} else if (i < R[i]) {
	    put[i] = pos+C[i]-step;
	    REP (t, step) {
		if (B[pos+C[i]-1-t]) {
		    if (t != C[i]) return 0;
		    break;
		} else if (t > C[i]) {
		    return 0;
		}
		B[pos+C[i]-1-t] = i;
	    }
	    if (L[i] < i) fr += put[i] - put[L[i]] - 1;
	} else {
	    REP (t, step) {
		if (B[pos+t]) {
		    if (t != C[i]) return 0;
		    break;
		} else if (t > C[i]) {
		    return 0;
		}
		B[pos+t] = i;
	    }
	    ans *= step - C[i] + 1;
	    fr += (R[i]!=1? put[R[i]]: N) - put[L[i]] - 2;
	}
    }
    return ans;
}

void MAIN() {
    scanf("%d", &N);
    REP (i, N) scanf("%d", A+i);

    Mint ans = 0;

    int prv = A[N-1];
    int pos = -1;
    REP (i, N) {
	if (A[i] == 1 && prv != 1) {
	    pos = i;
	    break;
	}
	prv = A[i];
    }

    if (*max_element(A, A+N) == 1) {
	ans = fact(N);
    } else if (pos == -1) {
	ans = 0;
    } else {
	REP (i, N) {
	    buf[i] = A[(i+pos)%N];
	}
	REP (i, N) A[i] = buf[i];
///	rprintf("%d", A, A+N);

	REP (i, N) C[A[i]]++;
	step = C[1];

	if (step < *max_element(C, C+N+1)) {
	    ans = 0;
	} else {
	    ans = calc();
	}
    }
    printf("%d\n", ans.geti());
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

