B - Bear and Snowstorm

4

21 votes
Hard
Problem

- Look at all these impassable roads.
- Nothing is impassable. Even "impassable" says...
- Dude, what?

Limak is a little polar bear. He lives in Bearland, close to the North Pole. You can try to imagine how much snow falls there every day.

Limak is responsible for the system of roads in Bearland. It's a hard task because he must deal with snow falling on roads. Yeah, polar bears use normal cars and their roads must be free of snow. As Limak is a very little bear, he needs your help!

There are n cities in Bearland, numbered 1 through n. There is a road between every two cities. Unfortunately, some of them are impassable because they're covered with snow. All other roads are passable.

Currently, Bearland is connected. It's possible to travel directly or indirectly between every two cities, using passable roads only. Maintaining roads is expensive, so only n1 of them are passable right now.

Below, a path denotes a sequence of distinct roads r1,r2,,rk that there exists a sequence of k+1 distinct cities v1,v2,,vk+1 that a road ri connects city vi and city vi+1, for 1ik.

Watch out because a huge snowstorm is coming. The weather forecast predicts that the snowstorm will move along a path of k passable roads, covering them with snow and thus making them impassable.

Bearland won't be connected anymore and Limak must fix it. He will rent a snowplow in one of the cities and drive along a path of k impassable roads, making them passable. Then Bearland should be connected again. Note that, in particular, Limak could choose the same path as a snowstorm did.

So, a set of k passable roads will be made impassable by the snowstorm. And then, a set of k impassable roads will be made passable by Limak. Note that some roads may belong to both sets.

Limak must consider all possible scenarios. We define a scenario as a pair of sets described in the previous paragraph. Two scenarios are the same only if the first set is the same in both scenarios and the second set is the same in both scenarios. Otherwise, scenarios are different.

You are given an integer k and the description of roads in Bearland. Find the number of different scenarios modulo 1000000007 (i.e. 109+7).

Input format

The first line of the input contains one integer T denoting the number of test cases.

The first line of each test case description contains two integers n and k denoting the number of cities in Bearland and the number of roads in a path.

The second line contains n1 integers p2,p3,,pn (1pii1) describing the current system of roads. There is a passable road between cities i and pi for every i{2,3,,n}. Under the given conditions Bearland is connected.

Output format

For each test case, output a single line containing the answer modulo 109+7.

Constraints

  • 1T5
  • 2n500000
  • 1kn1
  • 1pii1

Subtasks

Extra constraints Points Which tests
n6 10 1
n1000 10 2
k=1 10 3
every city is incident to at most 3 roads 20 4-5
no extra constraints 50 6-10
Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

The drawing below corresponds to the first sample test case. Bold lines denote passable roads, and dotted lines denote impassable roads. The initial situation is showed in a blue frame. A snowstorm will affect a path of k=2 passable roads, so there are three possible situations after that, showed in red frames. All three are isomorphic (equivalent), so we can count the answer for one of them, and multiply it by three. It turns out that Limak has exactly six ways to choose a path of k=2 impassable roads then and make Bearland connected again. All six ways showed in green frames. The answer is 63=18.

image sample test

Stack Limit for C++ is 8MB. You are allowed to increase it in your code, e.g. using setrlimit().

Editor Image

?