405397: GYM101933 K King's Colors
Description
Far, far away, there is the mystical Kingdom of Trees (more formally, "Royal Commonwealth of Connected Undirected Simple Acyclic Graphs"). The King there is very sad because his kingdom is not accepted as a sovereign state in the United Nations. In order to become a member, he needs to make a flag the UN can put on their website.
The flag will of course consist of the King's favorite tree, which contains $$$n$$$ nodes. The King would be happy to just have the tree colored in black and white, but for the sake of his wife the Queen, he decided that the tree will contain all the favorite colors of their $$$k$$$ children (they all have distinct favorite colors). Clearly, no two neighboring nodes can have the same color, but otherwise any coloring of the tree using exactly the $$$k$$$ colors would make a feasible flag candidate.
How many different flags are possible?
InputThe first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 2\,500$$$), where $$$n$$$ is the number of nodes in the King's favorite tree and $$$k$$$ is the number of children. Then follow $$$n-1$$$ lines describing the edges in the tree; the $$$i$$$'th of these lines contains a non-negative integer $$$p_i < i$$$, meaning that node $$$p_i$$$ is the parent of $$$i$$$.
The nodes are numbered from $$$0$$$ to $$$n-1$$$ and the tree is rooted at node $$$0$$$. Note that the embedding of the tree on the flag is already fixed, the only thing that remains is to assign colors.
OutputOutput the number of different possible color assignments. The number can be quite big, so the King has requested to know the answer modulo $$$1\,000\,000\,007$$$.
ExamplesInput4 3Output
0
1
1
18Input
6 4Output
0
1
1
3
4
600