编程介的小学生 2017-04-18 07:49 采纳率: 20.5%
浏览 847
已采纳

Fantastic Necklace

Alice is one of the most famous necklace designer. She is designing a fantastic nacklace 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 arbitarily. 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 necklace are counted as one if each bead conincides after rotating the necklace or rearranging the braches. But because of the special strings, two necklaces with mirroring arrangments 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

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-05-01 04:56
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题