CF BUDDY
← Problems·

1824B2 · LuoTianyi and the Floating Islands (Hard Version)

2300 · combinatorics, dfs and similar, math

Problem: This is the hard version of the problem. The only difference is that in this version knk\le n. You can make hacks only if both versions of the problem are solved.

Chtholly and the floating islands.

LuoTianyi now lives in a world with nn floating islands. The floating islands are connected by n1n-1 undirected air routes, and any two of them can reach each other by passing the routes. That means, the nn floating islands form a tree.

One day, LuoTianyi wants to meet her friends: Chtholly, Nephren, William, .... Totally, she wants to meet kk people. She doesn't know the exact positions of them, but she knows that they are in pairwise distinct islands. She define an island is good if and only if the sum of the distances^{\dagger} from it to the islands with kk people is the minimal among all the nn islands.

Now, LuoTianyi wants to know that, if the kk people are randomly set in kk distinct of the nn islands, then what is the expect number of the good islands? You just need to tell her the expect number modulo 109+710^9+7.

^{\dagger}The distance between two islands is the minimum number of air routes you need to take to get from one island to the other.

Input Format: The first line contains two integers nn and kk (1kn21051\le k \le n \le 2\cdot 10^5) — the number of the islands and people respectively.

Next n1n−1 lines describe the air routes. The ii-th of them contains two integers uiu_i and viv_i (1ui,vin,uivi1 \le u_i,v_i \le n, u_i \neq v_i) — the islands connected by the ii-th air route.

Output Format: Print a single integer — the expect number of the good islands modulo 109+710^9 + 7.

Formally, let M=109+7M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0q \not \equiv 0 (modM\operatorname{mod} M). Output the integer equal to pq1p \cdot q^{-1} modM\operatorname{mod} M. In other words, output such an integer xx that 0x<M0 \le x < M and xqpx \cdot q \equiv p (modM\operatorname{mod} M).

Note: In the first example the air routes form the following tree:

If the people are in the islands 11 and 22, then islands 11 and 22 will be good.

The sum of the distances from island 11 or 22 to all the people is 1+0=11+0=1, which is the minimal. While the sum of the distances from island 33 to all the people is 2+1=32+1=3, which is greater than 11.

Like this, when the people are in island 11 and 33, then islands 1,21,2 and 33 will be good.

When the people are in islands 11 and 44, then islands 1,2,31,2,3 and 44 will be good.

When the people are in islands 22 and 33, then islands 22 and 33 will be good.

When the people are in islands 22 and 44, then islands 2,32,3 and 44 will be good.

When the people are in islands 33 and 44, then islands 33 and 44 will be good.

So the expect of the number of the good islands is 166\frac{16}{6}, which equals to 666666674666666674 modulo 109+710^9+7.

In the second example the air routes form the following tree:

We can see that there is one person in each island, and only the island 33 is good. So the expect number is 11.

Sample Cases

Case 1

Input

4 2
1 2
2 3
3 4

Output

666666674

Case 2

Input

5 5
1 2
2 3
3 4
3 5

Output

1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback