Disgusting Relationship
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There are n people falling in love. Everybody has a unique people he/she loves, and somebody love themselves. But they have a secret agreement: no two people love the same one. To satisfy such a relationship between n people, we call it a , denoted by . Obviously, there are n! different for n people. And a can be presented as a permutation. For example, if n = 3, means the first people loves the third people, the second people loves the first people, the third people loves the second people.
if A loves B, and B loves C, and C loves A, then we call the relationship between these 3 people . and so on, we can easily define for each integer k. Extraordinary, if A loves B and B loves A, we call them . If A loves himself, we call it .

Apollo said proudly to Avery: If I konw "how many are there in this relationship" for each k, Then I can easily tell you how many relationship combinations might be legal for these n people.
But Avery said: No, It's not enough. Let's use f(a_1, a_2, a_3, ..., a_n) to denote the number of of n people, which can form a_1 , a_2 , ...,and a_n . If one relationship of n people can form a_1 , a_2 , ..., a_n , and f(a_1, a_2, a_3, ..., a_n) is divisible by my favourite number P, then I will feel the relationship is disgusting. I will tell you my favourite number P, and could you calculate how many relationships are disgusting between these n people? I know this might be too difficult to you, so you can just tell me how many of are disgusting.

: for relationships and , if will let there be a_k k-love polygons for each , will let there be b_k k-love polygons for each , and there exist at least one i, such that , then we call love relationships and are .

Apollo could not handle this task very well. Could you help him?

输入描述:

The first line is an integer  (), which is the number of test cases.

Each test case consists of one line with two integers n () and P (, P is a prime).

输出描述:

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is answer. Answer may be very large, so just output the answer mod 1,000,000,007.
示例1

输入

复制
4
4 2
4 3
4 5
5 2

输出

复制
Case #1: 2
Case #2: 2
Case #3: 5
Case #4: 2

说明

For n = 4, there are 5 different types of love relationships:
f(4, 0, 0, 0) = 1: \left[1, 2, 3, 4\right]
f(2, 1, 0, 0) = 6: \left[1, 2, 4, 3\right], \left[1, 3, 2, 4\right], \left[1, 4, 3, 2\right], \left[2, 1, 3, 4\right], \left[3, 2, 1, 4\right], \left[4, 2, 3, 1\right]
f(1, 0, 1, 0) = 8: \left[1, 3, 4, 2\right], \left[1, 4, 2, 3\right], \left[2, 3, 1, 4\right], \left[2, 4, 3, 1\right], \left[3, 2, 4, 1\right], \left[3, 1, 2, 4\right], \left[4, 2, 1, 3\right], \left[4, 1, 3, 2\right]
f(0, 2, 0, 0) = 3: \left[2, 1, 4, 3\right], \left[3, 4, 1, 2\right], \left[4, 3, 2, 1\right]
f(0, 0, 0, 1) = 6: \left[2, 3, 4, 1\right], \left[2, 4, 1, 3\right], \left[3, 1, 4, 2\right], \left[3, 4, 2, 1\right], \left[4, 3, 1, 2\right], \left[4, 1, 2, 3\right]

If P = 2, the first and the fourth type can meet the requirements.
If P = 3, the first and the third type can meet the requirements.
If P = 5, all types can meet the requirements.