#P9419. Sum of Permutation Sorting Costs

    ID: 22571 Type: Default 1000ms 256MiB

Sum of Permutation Sorting Costs

Sum of Permutation Sorting Costs

We are given a permutation of the numbers ({1,2,\ldots,n}) and a modulus (m). A special sorting process is applied to sort the permutation in increasing order by the following procedure:

  1. Operation: Let (k) be the first element of the permutation. Define [ t = \begin{cases} k-1, & \text{if } k \neq 1, \ n, & \text{if } k = 1. \end{cases} ] Then, locate (t) in the current permutation and move it to the beginning, shifting the intermediate elements one position to the right.

  2. The cost of an operation is the original index (0-indexed) of (t) in the permutation before it is moved.

  3. The cost of sorting a permutation is the sum of the costs for all operations performed until the permutation becomes sorted in increasing order ((1,2,\ldots,n)).

Your task is to compute the sum of the sorting costs for all (n!) permutations of ({1,2,\ldots,n}), and output the answer modulo (m).

inputFormat

The input consists of two integers (n) and (m), where (n) is the size of the permutation and (m) is the modulus.

outputFormat

Output a single integer: the sum of the sorting costs for all (n!) permutations modulo (m).

sample

1 100
0