You are given a tree with nodes (numbered from to ), and we consider its edges numbered from to (in the order in which they are given in the input). A tree is a connected, acyclic, undirected graph.
Let be a permutation of the numbers from to . For this given permutation , we assign to each edge of index a weight equal to . Then, consider an algorithm in which we initially have no edges in the tree, and we start adding them one by one, in the order of their numbering (essentially in the order in which they are given in the input, from to — the order of addition does not depend on the weights).
For the permutation to be considered valid, it is required that at every step of the algorithm, any maximal connected component (with at least nodes) formed so far has only edges with consecutive weights — here the components are considered individually (and only the maximal ones), so all of them must satisfy this condition, at every step of the algorithm.
Requirement
Given the number , representing the number of nodes in the tree, as well as the edges of the tree (in the order given in the statement), determine the minimum number of inversions of a valid permutation, as well as the number of valid permutations that achieve this minimum (modulo ).
Implementation Details
You must implement a single function:
std::pair<long long, int> solve(int N, std::vector<int> U, std::vector<int> V);
which receives as parameters:
- , the number of nodes in the tree
- The sequences and , where the pair represents the endpoints of edge in the tree (according to the numbering given in the statement), for from to .
The solve function will be called exactly once.
Constraints
- , for
- Note: Only the number of valid permutations with a minimum number of inversions is computed modulo . The minimum number of inversions itself is computed as an exact value.
| # | Score | Constraints |
|---|---|---|
| 1 | 2 | and the given tree has a star shape (there is a node of degree ) |
| 2 | 4 | |
| 3 | 11 | |
| 4 | 13 | |
| 5 | 21 | and the tree is a chain (all nodes have degree at most ) |
| 6 | 22 | |
| 7 | 27 | No additional restrictions. |
Example 1
input
7
3 7
2 5
2 6
1 3
1 2
2 4
stdout
2 2
Explanation
For the first example, the two optimal valid permutations that satisfy the constraints and achieve the minimum of inversions are:
Example 2
input
20
16 17
4 5
8 9
11 12
18 19
1 2
14 15
7 8
19 20
6 7
15 16
3 4
12 13
9 10
2 3
17 18
5 6
10 11
13 14
output
48 8
Explanation
For the second example, the minimum number of inversions of a valid permutation is , and there are valid permutations with this minimum number of inversions.