#ABC478F. 最小值优先搜索 / Min-First Search

最小值优先搜索 / Min-First Search

Problem Statement

For a tree TT with NN vertices called vertex 11, vertex 22, …\ldots, vertex NN, let f(T)f(T) be the permutation PP of (1,2,…,N)(1,2,\ldots,N) obtained by the following procedure.

  • Initially, let the sequence PP be the empty sequence ()(), and let the set SS be {1}\lbrace1\rbrace.
  • Repeat the following NN times.
  • Let xx be the minimum value of SS. Remove xx from SS, and append xx to the end of PP.
  • Then, let v1,v2,…,vkv_1, v_2, \ldots, v_k be the vertices adjacent to vertex xx in TT. For i=1,2,…,ki=1,2,\ldots,k, if viv_i is not contained in PP, add viv_i to SS.

You are given a permutation Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,…,N)(1,2,\ldots,N). Find the number, modulo 998244353998244353, of trees TT such that f(T)=Qf(T)=Q. Here, two trees are distinguished when there exists a pair of vertices (u,v)(u,v) such that there is an edge between vertex uu and vertex vv in one tree and there is no edge in the other tree.

Constraints

  • 1≤N≤2×1051\le N\le2\times10^5
  • 1≤Qi≤N (1≤i≤N)1\le Q_i\le N\ (1\le i\le N)
  • Qi≠Qj (1≤i<j≤N)Q_i\ne Q_j\ (1\le i\lt j\le N)
  • Q1=1Q_1=1
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

  • NN
  • Q1Q_1 Q2Q_2 …\ldots QNQ_N

Output

Output the number, modulo 998244353998244353, of trees TT such that f(T)=Qf(T)=Q.

5
1 3 4 2 5
8

For example, if TT is the following tree, then f(T)=Qf(T)=Q.

图示

The procedure to find f(T)f(T) proceeds as shown in the following figure.

图示

Including this tree, the following eight trees satisfy the condition.

图示

Thus, output 8.

20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
868586527

Output modulo 998244353998244353 the number of trees satisfying the condition.

30
1 16 23 14 18 10 12 21 27 30 17 28 4 9 20 13 22 11 19 3 25 26 6 7 2 8 5 24 15 29
910753763