#TENKA12018F. Circular

Circular

Score : 900900 points

Problem Statement

You are given a sequence of NN integers: A1,A2,...,ANA_1,A_2,...,A_N.

Find the number of permutations p1,p2,...,pNp_1,p_2,...,p_N of 1,2,...,N1,2,...,N that can be changed to A1,A2,...,ANA_1,A_2,...,A_N by performing the following operation some number of times (possibly zero), modulo 998244353998244353:

  • For each 1iN1\leq i\leq N, let qi=min(pi1,pi)q_i=min(p_{i-1},p_{i}), where p0=pNp_0=p_N. Replace the sequence pp with the sequence qq.

Constraints

  • 1N3×1051 \leq N \leq 3 \times 10^5
  • 1AiN1 \leq A_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1

::

ANA_N

Output

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

3
1
2
1
2

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

5
3
1
4
1
5
0
8
4
4
4
1
1
1
2
2
24
6
1
1
6
2
2
2
0