#P15691. [ICPC 2023 Jakarta R] Count BFS Graph

[ICPC 2023 Jakarta R] Count BFS Graph

题目描述

You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of N N nodes (numbered from 1 1 to N N ). The graph is represented by an adjacency matrix M M , for which node u u can traverse to node v v if Mu,v M_{u, v} is 1 1 , otherwise it is 0 0 . 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 A A such that A A is a permutation of 1 1 to N N and A1=1 A_1 = 1 . How many simple undirected graph with N N nodes and adjacency matrix M M such that BFS(M)=A \text{BFS}(M) = A ? Since the answer can be very large, calculate the answer modulo 998244353 998\,244\,353 .

A simple graph has no self-loop ( Mi,i=0 M_{i, i} = 0 for 1iN 1 \leq i \leq N ) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node u u is adjacent to node v v , then node v v is also adjacent to node u u ; formally, Mu,v=Mv,u M_{u, v} = M_{v, u} for 1u<vN 1 \leq u < v \leq 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 N N ( 2N5000 2 \leq N \leq 5000 ).

The second line consists of N N integers Ai A_i . The array A A is a permutation of 1 1 to N N and A1=1 A_1 = 1 .

输出格式

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

3
1 2 3
3
3
1 3 2
1
5
1 3 2 4 5
17
11
1 2 3 4 5 6 7 8 9 10 11
379394847

提示

Explanation for the sample input/output #1

The following illustration shows all graphs that satisfy the requirements.

:::align{center} :::

Explanation for the sample input/output #2

The only graph that satisfies the requirements is a graph with two edges: one that connects nodes 1 1 and 3 3 , and another one that connects nodes 3 3 and 2 2 .