atcoder#AGC043D. [AGC043D] Merge Triplets

[AGC043D] Merge Triplets

Score : 12001200 points

Problem Statement

Given is a positive integer NN. Find the number of permutations (P1,P2,,P3N)(P_1,P_2,\cdots,P_{3N}) of (1,2,,3N)(1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number MM.

  • Make NN sequences A1,A2,,ANA_1,A_2,\cdots,A_N of length 33 each, using each of the integers 11 through 3N3N exactly once.
  • Let PP be an empty sequence, and do the following operation 3N3N times.- Among the elements that are at the beginning of one of the sequences AiA_i that is non-empty, let the smallest be xx.
    • Remove xx from the sequence, and add xx at the end of PP.
  • Among the elements that are at the beginning of one of the sequences AiA_i that is non-empty, let the smallest be xx.
  • Remove xx from the sequence, and add xx at the end of PP.

Constraints

  • 1N20001 \leq N \leq 2000
  • 108M109+710^8 \leq M \leq 10^9+7
  • MM is a prime number.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

Output

Print the number of permutations modulo MM.

1 998244353
6

All permutations of length 33 count.

2 998244353
261
314 1000000007
182908545