logo AlgoBeat OnlineJudge
登录 注册

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

内存限制:1024 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of nodes (numbered from to ). The graph is represented by an adjacency matrix , for which node can traverse to node if is , otherwise it is . 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 such that is a permutation of to and . How many simple undirected graph with nodes and adjacency matrix such that ? Since the answer can be very large, calculate the answer modulo .

A simple graph has no self-loop ( for ) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node is adjacent to node , then node is also adjacent to node ; formally, for .

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 ( ).

The second line consists of integers . The array is a permutation of to and .

输出格式

Output an integer representing the number of simple undirected graphs with nodes and adjacency matrix such that . Since the answer can be very large, output the answer modulo .

样例

样例输入 1

3
1 2 3

样例输出 1

3

样例输入 2

3
1 3 2

样例输出 2

1

样例输入 3

5
1 3 2 4 5

样例输出 3

17

样例输入 4

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

样例输出 4

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 and , and another one that connects nodes and .