#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, M;
  cin >> N >> M;

  const int ZERO = 3 * N + 2;
  vector<vector<int>> dp(3 * N + 1, vector<int>(6 * N + 10));

  dp[3 * N][ZERO] = 1;
  for (int i = 3 * N; i > 0; i--) { // construct P backwards
    for (int j = 0; j < 6 * N + 5; j++) {
      dp[i - 1][j + 1] = (dp[i - 1][j + 1] + dp[i][j]) % M; // Make block of size 1 (highest)
      if (i >= 2 && j >= 1) {
        // Make block of size 2 (the first element is the highest, the second one is free), 
        // put it after a block 1
        dp[i - 2][j - 1] = (dp[i - 2][j - 1] + (1ll * (i - 1) * dp[i][j] % M)) % M; 
      }
      if (i >= 3) {
        dp[i - 3][j] = (dp[i - 3][j] + (1ll * (i - 1) * (i - 2) % M * dp[i][j] % M)) % M; // Make block of size 3
      }
    }
  }

  int ans = 0;
  for (int i = ZERO; i < 6 * N + 5; i++) {
    ans = (ans + dp[0][i]) % M;
  }
  cout << ans << "\n";
  return 0;
}
