PivotOJ

Count BFS Graph

시간 제한: 2000ms메모리 제한: 1024MB출처: ICPC Asia Jakarta Regional 2023BOJ 32085

문제

You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of NN nodes (numbered from 11 to NN). The graph is represented by an adjacency matrix MM, for which node uu can traverse to node vv if Mu,vM_{u,v} is 11, otherwise it is 00. Your algorithm will output the order the nodes are visited in the BFS. The pseudocode of the algorithm is presented as follows.

BFS(M[1..N][1..N]):
    let A be an empty array
    let Q be an empty queue

    append 1 to A
    push 1 to Q

    while Q is not empty:
        pop the front element of Q into u
        for v = 1 to N:
            if M[u][v] == 1 and v is not in A:
                append v to A
                push v to Q

    return A

During your research, you are interested in the following problem. Given an array AA such that AA is a permutation of 11 to NN and A1=1A_1 = 1. How many simple undirected graph with NN nodes and adjacency matrix MM such that BFS(M)=A\text{BFS}(M) = A? Since the answer can be very large, calculate the answer modulo 998244353998\, 244\, 353.

A simple graph has no self-loop (Mi,i=0M_{i,i} = 0 for 1 &le; i &le; N) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node uu is adjacent to node vv, then node vv is also adjacent to node uu; formally, Mu,v=Mv,uM_{u,v} = M_{v,u} for 1 &le; u < v &le; N.

Two graphs are considered different if there is an edge that exists in one graph but not the other. In other words, two graphs are considered different if their adjacency matrices are different.

입력

The first line consists of an integer NN (2 &le; N &le; 5000).

The second line consists of NN integers AiA_i. The array AA is a permutation of 11 to NN and A1=1A_1 = 1.

출력

Output an integer representing the number of simple undirected graphs with NN nodes and adjacency matrix MM such that BFS(M)=A\text{BFS}(M) = A. Since the answer can be very large, output the answer modulo 998244353998\, 244\, 353.

예제

예제 1

입력
3
1 2 3
출력
3

예제 2

입력
3
1 3 2
출력
1

예제 3

입력
5
1 3 2 4 5
출력
17

예제 4

입력
11
1 2 3 4 5 6 7 8 9 10 11
출력
379394847
코드를 제출하려면 로그인하세요.