#abc252g. [abc252_g]Pre-Order

[abc252_g]Pre-Order

Problem Statement

There is a rooted tree with NN vertices called Vertex 11, 22, ldots\\ldots, NN, rooted at Vertex 11.

We performed a depth-first search starting at the root and obtained a preorder traversal of the tree: P1,P2,ldots,PNP_1, P_2, \\ldots, P_N.
During the search, when the current vertex had multiple children, we chose the unvisited vertex with the smallest index.

What is a preorder traversal?

We start at the root and repeat the following procedure to list the vertices of the tree.* If the current vertex uu is not recorded yet, record it.

  • Then, if uu has an unvisited vertex, go to that vertex.
  • Otherwise, terminate if uu is the root, and go to the parent of uu if it is not. The list of vertices in the order they are recorded here is the preorder traversal of the tree.

Find the number of rooted trees consistent with the preorder traversal, modulo 998244353998244353.
Two rooted trees (with NN vertices and rooted at Vertex 11) are considered different when there is a non-root vertex whose parent is different in the two trees.

Constraints

  • 2leqNleq5002 \\leq N \\leq 500
  • 1leqPileqN1 \\leq P_i\\leq N
  • P1=1P_1=1
  • All PiP_i are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN P1P_1 P2P_2 ldots\\ldots PNP_N

Output

Print the number of rooted trees consistent with the preorder traversal, modulo 998244353998244353.


Sample Input 1

4
1 2 4 3

Sample Output 1

3

The rooted trees consistent with the preorder traversal are the three shown below, so the answer is 33.

Note that the tree below does not count. This is because, among the children of Vertex 22, we visit Vertex 33 before visiting Vertex 44, resulting in the preorder traversal 1,2,3,41,2,3,4.


Sample Input 2

8
1 2 3 5 6 7 8 4

Sample Output 2

202