PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000084. Bài tập hoán vị (Exercise)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bài tập hoán vị (Exercise)

Exercise

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN distinct cows initially standing in positions 11 through NN. Farmer John chooses a fixed permutation AA of 1,…,N1,\ldots,N. In every step, the cow currently at position ii moves to position AiA_i. The same permutation is used repeatedly.

Let K(A)K(A) be the smallest positive number of steps after which the cows return to their initial arrangement. In particular, the identity permutation has K(A)=1K(A)=1, not 00.

Consider all distinct positive integers kk such that at least one permutation on NN elements has K(A)=kK(A)=k. Compute the sum of these distinct values, not the number of permutations giving them. Print the result modulo the given prime MM.

Every permutation decomposes into disjoint cycles. A cycle of length ll returns to its original state after ll steps, and hence K(A)K(A) is the least common multiple of all cycle lengths.

Input

A single line contains two integers NN and MM.

Output

Print the sum of all distinct attainable permutation orders, modulo MM.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le30; MM is prime and 108≤M≤109+710^8\le M\le10^9+7.

  • Subtask 2 (30 points): 1≤N≤10001\le N\le1000; MM is prime and 108≤M≤109+710^8\le M\le10^9+7.

  • Subtask 3 (50 points): 1≤N≤1041\le N\le10^4; MM is prime and 108≤M≤109+710^8\le M\le10^9+7.

Examples

Example 1

Input

5 1000000007

Output

21

Explanation: The attainable distinct orders are 1, 2, 3, 4, 5 and 6, whose sum is 21.

Example 2

Input

3 998244353

Output

6

Explanation: The only possible orders for three elements are 1, 2 and 3, with sum 6.