OI Problems   关于

#lc9j17. Partial Sorting

时间限制:1 s       空间限制:1 GiB       标签: 数学 组合数学 组合计数 CodeForces 

算法难度等级:0       思维难度等级:0       实现难度等级:0


本题来源于:Codeforces Round 842 (Div. 2) Problem E

  • 简体中文
  • English

题目描述

给定整数 nn 和质数 MM
对于一个 13n1\sim 3n 的排列 pp,你可以进行下列操作:

  • pp 的前 2n2n 个元素从小到大排序。
  • pp 的后 2n2n 个元素从小到大排序。

可以证明任意 13n1\sim 3n 的排列都能通过若干次上述操作从小到大排序。
f(p)f(p) 表示将 pp 从小到大排序所需的最小总操作次数。
你需要求出对于所有 13n1\sim 3n 的排列 pp,其 f(p)f(p) 总和对 MM 取模后的值。

输入格式

输入一行两个整数 n,Mn,M1n1061\leq n\leq 10^6108M10910^8\leq M\leq 10^9),保证 MM 是质数。

输出格式

输出一行一个整数表示 f(p)f(p) 的总和对 MM 取模后的值。

1 100009067
9
2 100000357
1689
69 999900997
193862705

提示

In the first test case, all the permutations are:

  • [1,2,3][1, 2, 3], which requires 00 operations;
  • [1,3,2][1, 3, 2], which requires 11 operation;
  • [2,1,3][2, 1, 3], which requires 11 operation;
  • [2,3,1][2, 3, 1], which requires 22 operations;
  • [3,1,2][3, 1, 2], which requires 22 operations;
  • [3,2,1][3, 2, 1], which requires 33 operations.

Therefore, the answer is 0+1+1+2+2+3=90+1+1+2+2+3=9.

Consider a permutation p^\dagger p of length 3n3n . Each time you can do one of the following operations:

  • Sort the first 2n2n elements in increasing order.
  • Sort the last 2n2n elements in increasing order.

We can show that every permutation can be made sorted in increasing order using only these operations. Let's call f(p)f(p) the minimum number of these operations needed to make the permutation pp sorted in increasing order.

Given nn , find the sum of f(p)f(p) over all (3n)!(3n)! permutations pp of size 3n3n .

Since the answer could be very large, output it modulo a prime MM.

^\dagger A permutation of length nn is an array consisting of nn distinct integers from $ 1 $ to $ n $ in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The only line of input contains two numbers nn and MM (1n1061 \leq n \leq 10^6, 108M10910^8 \leq M \leq 10^9). It is guaranteed that MM is a prime number.

Output

Output the answer modulo MM.

1 100009067
9
2 100000357
1689
69 999900997
193862705

Tip

In the first test case, all the permutations are:

  • [1,2,3][1, 2, 3], which requires 00 operations;
  • [1,3,2][1, 3, 2], which requires 11 operation;
  • [2,1,3][2, 1, 3], which requires 11 operation;
  • [2,3,1][2, 3, 1], which requires 22 operations;
  • [3,1,2][3, 1, 2], which requires 22 operations;
  • [3,2,1][3, 2, 1], which requires 33 operations.

Therefore, the answer is 0+1+1+2+2+3=90+1+1+2+2+3=9.