ROOMALLOC
The students of a school class are planning their five-day trip. The class consists of students numbered from to . The hotel they will stay at has rooms, numbered from to . Each student will be assigned to one of these rooms.
As you may know, on school trips students are more interested in staying with their friends - they are not particularly concerned about the capacity of the rooms. Therefore, in this problem, we assume that all students can fit into the same room.
The personal relationships among the students are complex. Some students have exactly one other student whom they dislike, and they will not end up in the same room as that student.
Your task is to find out how many different ways the students can be assigned to rooms. Two assignments are considered different if at least one student ends up in a different room.
Input:
The first line of the input will contain two positive integers and , separated by a single space: the number of students and the number of rooms, respectively. The next line will contain integers , , ..., , separated in pairs by spaces, such that . If , it means that student dislikes student and will not end up in the same room as them. If , it means that student does not dislike any other student.
Output:
The output must contain one line with exactly one integer: the number of possible ways the students can end up in the rooms. Since this number can be very large, you should print its remainder when divided (modulo) by the number .
Constraints:
- Time Limit: sec.
- Memory Limit: MB.
- For test cases with a total value of , it will be .
- For test cases with a total value of , it will be and .
- For test cases with a total value of , the numbers will all be distinct.
Examples of input - output:
1st
input:
2 3
2 1
output:
6
Explanation of the first example:
There are students and rooms available. Since the students dislike each other, they must go to different rooms. The possible assignments are: , , , , , , where the numbers inside the parentheses represent the rooms assigned to the two students, respectively.
2nd
input:
3 4
2 3 1
output:
24
3d
input:
3 4
2 1 1
output:
36
Comments