F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Fantastic Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 241    Accepted Submission(s): 23


Problem Description
Alice is one of the most famous necklace designers. She is designing a fantastic necklace of new styles. Like other necklaces, this necklace consists of n beads and n strings connecting the bead together. The most significant difference is that the beads are not connected one by one but has many branches. The branches connecting to the same bead can be arranged arbitrarily. The shaking of the branches makes the necklace more glaring. Another difference is that the beads are not connected with ordinary strings. The two ends of each string differ, which makes the necklace more unique.
She has finished the structure of the new necklace. However, she is hesitating what color of bead should be used for each position. As a famous designer, she has beads of m colors and each type has infinite number of beads. She wants to make a necklace that any pair of adjacent beads has different colors. Now can you tell her how many different necklaces can she make with the colorful beads? Two necklaces are counted as one if each bead coincides after rotating the necklace or rearranging the braches. But because of the special strings, two necklaces with mirroring arrangements are not considered the same.
 

Input
The input consists of about 50 test cases. The first line of each test case contains two integers, n and m, the number of beads of the necklace and the number of colors of beads Alice has (2 < n ¡Ü 100000, 1 ¡Ü m ¡Ü 100000). The second line contains n integers, the i-th of which, named pi, means that there is a string from the i-th bead to the pi-th bead (1 ¡Ü pi ¡Ü n and pi does not equal to i). The necklace is always valid and there is no duplicated string connecting the same pair of beads.
 

Output
For each test case, output the number of different necklaces that Alice can make in one line. For the answer is large, so please give the result of answer module 1,000,000,007.
 

Sample Input
3 3 2 3 1 3 2 2 3 1 6 3 2 3 1 1 2 3 6 2 2 3 4 1 1 3 6 3 2 3 1 1 4 4
 

Sample Output
2 0 16 2 36
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-04 17:02:58, Gzip enabled