Problem:
Score : 900 points

Problem Statement

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.

Sample Input 1

3
1
2
1

Sample Output 1

2

(2,3,1) and (3,2,1) satisfy the condition.

Sample Input 2

5
3
1
4
1
5

Sample Output 2

0

Sample Input 3

8
4
4
4
1
1
1
2
2

Sample Output 3

24

Sample Input 4

6
1
1
6
2
2
2

Sample Output 4

0

Solution in Python (3.4.3) (judge verdict: Time Limit Exceeded)

import numpy as np
import sys

M = 998244353
N = int(input())
A = np.empty(N, dtype=np.int64)
for i in range(N):
    A[i] = int(input()) - 1
    # A[i]: 0,1,...,(N-1)

s=0
if A[0]==A[N-1]:
    while (s <= N-1) and (A[s]==A[0]):
        s = s+1
if s == N:
    if A[0] == 0:
        result = 1
        for i in range(1,N+1):
            result = (result*i) % M
        print(result)
    else:
        print(0)

    sys.exit()

lengths = np.zeros(N, dtype=np.int64)
heads = np.zeros(N, dtype=np.int64)
tails = np.zeros(N, dtype=np.int64)

if s > 0:
    tail = A[0]
else:
    tail = A[N-1]

l = 1
for i in range(s,N):
    if (i < N-1) and (A[i+1] == A[i]):
        l = l+1
    else:
        if i < N-1:
            if lengths[A[i+1]] > 0:
                print(0)
                sys.exit()
            else:
                heads[A[i]] = A[i+1]
        else:
            heads[A[i]] = A[0]

        lengths[A[i]] = l
        l=1
        tails[A[i]] = tail
        tail = A[i]
if s > 0:
    heads[A[0]] = A[s]
    lengths[A[0]] = lengths[A[0]] + s

k = lengths[0]
if k == 0:
    print(0)
    sys.exit()

result = 1
killed = 0
for i in range(1,N):
    if lengths[i] == 0:
        result = (result * killed) % M
        killed = killed - 1
    elif lengths[i] > k:
        print(0)
        exit()
    elif heads[i] > i and tails[i] > i:
        if lengths[i] != k:
            print(0)
            exit()
    elif heads[i] < i and tails[i] < i:
        result = (result * (k-lengths[i]+1)) % M
        killed = killed + (lengths[i]+k-2)
    else:
        # head < i and tail > i
        # head > i and tail < i
        killed = killed + (lengths[i]-1)

print(result)