Problem:
Score : 1200 points

Problem Statement

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

Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once.

Let P be an empty sequence, and do the following operation 3N times.

Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x.

Remove x from the sequence, and add x at the end of P.

Constraints

1 \leq N \leq 2000

10^8 \leq M \leq 10^9+7

M is a prime number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of permutations modulo M.

Sample Input 1

1 998244353

Sample Output 1

6

All permutations of length 3 count.

Sample Input 2

2 998244353

Sample Output 2

261

Sample Input 3

314 1000000007

Sample Output 3

182908545

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Arrays;
import java.util.Scanner;

public final class Main {
	private static final Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int N = sc.nextInt();
		long M = sc.nextInt();
		long[][][] dp = new long[3][N + 1][N + 1]; // dp[# of 1][# of 3][# of 2]
		dp[0][0][0] = 1;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j + i <= N; j++) {
				long rest = 3 * N - 3 * i - 2 * j;
				{
					// no 1
					// 0 -> 3
					dp[0][i + 1][j] += dp[0][i][j] * (rest - 1) % M * (rest - 2);
					dp[0][i + 1][j] %= M;
					if (j < N) {
						// 0 -> 2
						dp[0][i][j + 1] += dp[0][i][j] * (rest - 1);
						dp[0][i][j + 1] %= M;
					}
					if (j == 0) {
						// 0 -> 1
						dp[1][i][j] += dp[0][i][j];
						dp[1][i][j] %= M;
					} else {
						// 2 -> 3
						dp[0][i + 1][j - 1] += dp[0][i][j];
						dp[0][i + 1][j - 1] %= M;
					}
				}
				{
					// has one 1
					if (i + j + 1 < N) {
						// 0 -> 3
						dp[1][i + 1][j] += dp[1][i][j] * (rest - 2) % M * (rest - 3);
						dp[1][i + 1][j] %= M;
						if (j == 0) {
							// 0 -> 1
							dp[2][i][j] += dp[1][i][j];
							dp[2][i][j] %= M;
						}
					}
					// 1 -> 3
					dp[0][i + 1][j] += dp[1][i][j] * (rest - 2);
					dp[0][i + 1][j] %= M;
					if (j == 0) {
						// 1 -> 2
						dp[0][i][j + 1] += dp[1][i][j];
						dp[0][i][j + 1] %= M;
					} else {
						// 2 -> 3
//						dp[1][i + 1][j - 1] += dp[1][i][j];
//						dp[1][i + 1][j - 1] %= M;
					}
				}
				{
					// has two 1
					if (i + j + 2 < N) {
						// 0 -> 3
						dp[2][i + 1][j] += dp[2][i][j] * (rest - 3) % M * (rest - 4);
						dp[2][i + 1][j] %= M;
					}
					// 1 -> 3
					dp[1][i + 1][j] += dp[2][i][j] * (rest - 3);
					dp[1][i + 1][j] %= M;
					if (j == 0) {
						// 1 -> 2
//						dp[1][i][j + 1] += dp[2][i][j];
//						dp[1][i][j + 1] %= M;
					} else {
						// 2 -> 3
//						dp[2][i + 1][j - 1] += dp[2][i][j];
//						dp[2][i + 1][j - 1] %= M;
					}
				}
			}
		}
		System.out.println(dp[0][N][0]);
	}
}