Problem p02738 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}

LL kai[6600];

long long int pow_mod(long long int p_a, long long int p_n,
                      long long int p_p = 1e9 + 7) {
  // p_a^p_n mod p_p
  long long int p_b = 1, p_t = 1;
  for (; p_b <= p_n; p_b <<= 1)
    ;
  for (p_b >>= 1; p_b > 0; p_b >>= 1) {
    p_t *= p_t;
    if (p_t >= p_p)
      p_t %= p_p;
    if (p_n & p_b)
      p_t *= p_a;
    if (p_t >= p_p)
      p_t %= p_p;
  }
  return p_t;
}

long long int gcd(long long int g_a, long long int g_b) {
  if (g_a < g_b)
    gcd(g_b, g_a);
  if (g_b == 0)
    return g_a;
  return gcd(g_b, g_a % g_b);
}

long long int divide(long long int d_a, long long int d_b,
                     long long int d_p = 1e9 + 7) {
  // d_a/d_b mod d_p
  // prime:d_p is prime
  if (d_a >= d_p)
    d_a %= d_p;
  if (d_a < 0)
    d_a += d_p;
  if (d_b >= d_p)
    d_b %= d_p;
  if (d_b < 0)
    d_b += d_p;
  d_a *= pow_mod(d_b, d_p - 2, d_p);
  return d_a % d_p;
}

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c;
  LL p, q, r;
  LL s = 0;
  cin >> n >> m;
  kai[0] = 1;
  for (i = 1; i <= 3 * n; i++) {
    kai[i] = kai[i - 1] * i % m;
  }
  for (i = 0, p = 1; i <= n; i++) {
    for (j = 0, q = 1; i + j <= n; j++) {
      a = divide(kai[3 * n], p, m);
      a = divide(a, q, m);
      a = divide(a, kai[i], m);
      a = divide(a, kai[j], m);
      a = divide(a, kai[3 * n - 3 * i - 2 * j], m);
      s += a;
      if (s >= m)
        s %= m;

      q *= 2;
      if (q >= m)
        q %= m;
    }
    p *= 3;
    if (p >= m)
      p %= m;
  }
  cout << s << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}

LL kai[6600];

long long int pow_mod(long long int p_a, long long int p_n,
                      long long int p_p = 1e9 + 7) {
  // p_a^p_n mod p_p
  long long int p_b = 1, p_t = 1;
  for (; p_b <= p_n; p_b <<= 1)
    ;
  for (p_b >>= 1; p_b > 0; p_b >>= 1) {
    p_t *= p_t;
    if (p_t >= p_p)
      p_t %= p_p;
    if (p_n & p_b)
      p_t *= p_a;
    if (p_t >= p_p)
      p_t %= p_p;
  }
  return p_t;
}

long long int gcd(long long int g_a, long long int g_b) {
  if (g_a < g_b)
    gcd(g_b, g_a);
  if (g_b == 0)
    return g_a;
  return gcd(g_b, g_a % g_b);
}

long long int divide(long long int d_a, long long int d_b,
                     long long int d_p = 1e9 + 7) {
  // d_a/d_b mod d_p
  // prime:d_p is prime
  if (d_a >= d_p)
    d_a %= d_p;
  if (d_a < 0)
    d_a += d_p;
  if (d_b >= d_p)
    d_b %= d_p;
  if (d_b < 0)
    d_b += d_p;
  d_a *= pow_mod(d_b, d_p - 2, d_p);
  return d_a % d_p;
}

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c;
  LL p, q, r;
  LL s = 0;
  cin >> n >> m;
  kai[0] = 1;
  for (i = 1; i <= 3 * n; i++) {
    kai[i] = kai[i - 1] * i % m;
  }
  for (i = 0, p = 1; i <= n; i++) {
    for (j = 0, q = 1; i + j <= n; j++) {
      b = p * q % m;
      b = b * kai[i] % m;
      b = b * kai[j] % m;
      b = b * kai[3 * n - 3 * i - 2 * j] % m;
      a = divide(kai[3 * n], b, m);
      s += a;
      if (s >= m)
        s %= m;

      q *= 2;
      if (q >= m)
        q %= m;
    }
    p *= 3;
    if (p >= m)
      p %= m;
  }
  cout << s << endl;
}