#P9419. [POI 2021/2022 R1] Układanie kart

[POI 2021/2022 R1] Układanie kart

Background

Translated from XXIX Olimpiada Informatyczna – Stage I Układanie kart

Problem Description

We sort a permutation into increasing order using the following method.

One operation: Let the first number be kk. Find k1k-1 in the permutation (if k=1k=1, take nn). Move k1k-1 to the first position of the permutation, and shift the numbers in between one position to the right.

Cost of one operation: the position of k1k-1 (or nn) in the original permutation (indexed starting from 00).

Cost of a permutation: perform several operations until the permutation is sorted. The cost is the sum of the costs of all operations.

Given n,mn,m, find the sum of the costs over all n!n! permutations, modulo mm.

Input Format

One line with two positive integers, n,mn,m.

Output Format

One line with one integer, the answer modulo mm.

2 100

1

3 100

15

10 1000

100

500 100000

60000

100000 1000

0

Hint

For all testdata, 2n10000002\leq n\leq 10000002m1092\leq m\leq 10^9

Subtask ID Additional Constraints Score
1 n10n\leq 10 10
2 n2000n\leq 2000 60
3 30

Translated by ChatGPT 5