You are given an (undirected) graph with vertices and edges. You are also given a tree with vertices. The vertices of the tree and of the graph are numbered .
You need to find the number of permutations of such that if is an edge of tree , is an edge of graph . The converse doesn't have to be true: it is possible that if is an edge of , is not an edge of tree .
Input Specification
The first line of the input consists of two positive integers denoting the number of nodes and the number of edges in .
In the following lines, each line consists of two positive integers denoting that there is an edge in .
In the following lines, each line consists of two positive integers denoting that there is an edge in .
Output Specification
Output the number of valid permutations satisfying the above constraints. If there is no such permutation , output .
Sample Input 1
5 4
1 2
1 3
1 4
1 5
3 1
3 2
3 4
3 5
Sample Output 1
24
Explanation
We must map vertex of the tree to vertex of the graph . Afterwards, we can simply map vertices of to vertices of in any order. So the answer is .
Sample Input 2
6 12
1 2
1 4
1 5
1 6
2 3
2 4
2 6
3 4
3 5
3 6
4 5
4 6
3 4
4 1
1 6
6 5
4 2
Sample Output 2
196
Constraints
For all test cases, .
It is guaranteed that there are no parallel edges or self loops in graph .
- Subtask 1 (14 points): .
- Subtask 2 (10 points): it is guaranteed that is a path: equivalently, it is a tree whose maximum degree is at most 2. A path with vertices has edges .
- Subtask 3 (10 points): . Depends on Subtask 1.
- Subtask 4 (15 points): , and the answer is at most .
- Subtask 5 (16 points): . Depends on Subtask 1,3,4.
- Subtask 6 (35 points): no additional constraints. Depends on all previous subtasks.
Comments