echipe

Time limit: 0.2s Memory limit: 128MB Input: echipe.in Output: echipe.out

You are the owner of a football team. You have found the perfect strategy to win any match your team will play. This strategy consists of instructing each player to pass the ball to exactly one person. Although it initially seems like a poor strategy, it has helped you reach the finals of the village championship.

Within a team, sub-teams are formed. A sub-team is a group of players in which any player can pass to any other player through an indirect pass or a series of passes. In these sub-teams you have established certain roles that at least one player must have, specifically KK different roles.

Task

Knowing that there are NN players on the team, KK different roles, and each pass that a player can make, determine the number of ways to assign roles to the team.

Input data

The first line of the input file echipe.in contains NN followed by KK. The next line contains a sequence of natural numbers p1p_1, p2p_2, \cdots, pNp_N. For each ii, 1iN1 \leq i \leq N, player ii can pass to player pip_i.

Output data

The first line of the output file echipe.out will contain a single integer, representing the remainder of the number of ways to assign roles to the team divided by 109+710^9 + 7.

Constraints and clarifications

  • 1KN200 0001 \leq K \leq N \leq 200\ 000
  • Each number in the interval [1,N][1, N] appears in the sequence pp only once
  • It is guaranteed that there is at least one way to assign roles to the team

Subtasks

# Points Constraints
1 12 1N151 \leq N \leq 15, 1K41 \leq K \leq 4
2 40 1N,K1 0001 \leq N, K \leq 1\ 000
3 48 No additional constraints

Example

echipe.in

5 2
2 3 1 5 4

echipe.out

12

Explanation

The formed sub-teams are: {1, 2, 3}\{ \texttt{1, 2, 3} \} and {4, 5}\{ \texttt{4, 5} \}.

The passes the players will make look like this, and the ways to assign roles are as follows (where rir_i indicates the role of player ii):

# r₁ r₂ r₃ r₄ r₅
1 1 1 2 1 2
2 1 1 2 2 1
3 1 2 1 1 2
4 1 2 1 2 1
5 1 2 2 1 2
6 1 2 2 2 1
7 2 1 1 1 2
8 2 1 1 2 1
9 2 1 2 1 2
10 2 1 2 2 1
11 2 2 1 1 2
12 2 2 1 2 1

Log in or sign up to be able to send submissions!