#CF2239D. 猎兽 / D. Hunting the Beast
猎兽 / D. Hunting the Beast
D. Hunting the Beast
Source: Codeforces 2239D
Contest: Codeforces Round 1105 (Div. 1)
Time limit: 2 seconds
Memory limit: 256 megabytes
Statement
In Zhuji, a city in central Zhejiang Province, local folklore tells of a wild beast known as the Modeiyon. Dwelling deep in the mountains, it is said to sneak into villages at night to devour livestock and prey on lone travelers. Though many elders claim to have seen it, no photograph of the creature has ever been taken.
A brave group of people decides to head up the mountain to hunt the beast. The mountain's locations and trails can be modeled as a functional graph with vertices (numbered to ). A functional graph is a directed graph with vertices and edges, where every vertex has an out-degree of exactly . Additionally, it is known that the mountain's trails do not ever form self-loops.
The group will choose exactly distinct vertices to form their initial starting set . A starting set is defined as successful if every vertex in the graph is reachable from at least one vertex (a vertex is always reachable from itself).
There are possible ways to choose the starting set of size . They define the value of a graph as the number of successful starting sets it has.
However, the exact layout of the mountain's trails is unknown. If the destination of the single outgoing edge from each vertex is chosen arbitrarily from the remaining vertices (excluding the vertex itself), there are exactly possible functional graphs. Given and , your task is to calculate the sum of the values of all possible functional graphs. Since the answer can be very large, print it modulo .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The only line of each test case contains two integers () — the number of vertices in the graph and the number of starting vertices.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the sum of the values of all possible functional graphs, modulo .
Note
In the first test case, there is exactly valid functional graph: and . Both starting sets of size ( and ) can reach all vertices. Thus, the total value is .
In the second test case, there are valid functional graphs. They can be categorized as follows:
- graphs form a single cycle of length (e.g., ). Starting at any of the vertices can reach all vertices. This contributes to the total value.
- graphs consist of a -cycle and a single leaf pointing to it (e.g., and ). To reach all vertices, the starting set must be exactly the leaf vertex. This contributes to the total value.
The total sum of values is . In the third test case, the possible graphs are the same, but we choose subsets of size :
- For the cycle graphs, any subset of size is successful. This contributes .
- For the leaf graphs, the subset must contain the leaf vertex. There are exactly such subsets for each graph. This contributes .
The total sum of values is .
Examples
5
2 1
3 1
3 2
4 2
8 3
2
12
18
216
20415360