Problem p02738 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
// #define i_7 (ll)(1E9+7)
// #define i_7 998244353
// #define i_5 i_7-2
ll i_7;
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////

int main() {
  fastio

      ll n;
  cin >> n >> i_7;
  ll ofs = 10000;
  ll dp[4][ofs * 2];
  memset(dp, 0, sizeof(dp));
  dp[0][ofs] = 1;
  rep(i, 1, 3 * n) {
    rep(j, 0, 2 * ofs - 1) { dp[i & 3][j] = 0; }
    rep(j, 0, 2 * ofs - 1) {
      if (i - 1 >= 0 && j + 1 <= 2 * ofs - 1)
        Add(dp[i & 3][j], dp[(i + 3) & 3][j + 1]);
      if (i - 2 >= 0 && j - 1 >= 0)
        Add(dp[i & 3][j], mod(dp[(i + 2) & 3][j - 1] * (3 * n - i + 1)));
      if (i - 3 >= 0)
        Add(dp[i & 3][j],
            mod(dp[(i + 1) & 3][j] * mod((3 * n - i + 2) * (3 * n - i + 1))));
    }
  }
  ll ans = 0;
  rep(j, 0, ofs) Add(ans, dp[(3 * n) & 3][j]);
  cout << mod(ans) << endl;

  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
// #define i_7 (ll)(1E9+7)
// #define i_7 998244353
// #define i_5 i_7-2
ll i_7;
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////

int main() {
  fastio

      ll n;
  cin >> n >> i_7;
  ll ofs = 6005;
  ll dp[4][ofs * 2];
  memset(dp, 0, sizeof(dp));
  dp[0][ofs] = 1;
  rep(i, 1, 3 * n) {
    rep(j, 0, 2 * ofs - 1) { dp[i & 3][j] = 0; }
    rep(j, 0, 2 * ofs - 1) {
      if (i - 1 >= 0 && j + 1 <= 2 * ofs - 1)
        Add(dp[i & 3][j], dp[(i + 3) & 3][j + 1]);
      if (i - 2 >= 0 && j - 1 >= 0)
        Add(dp[i & 3][j], mod(dp[(i + 2) & 3][j - 1] * (3 * n - i + 1)));
      if (i - 3 >= 0)
        Add(dp[i & 3][j],
            mod(dp[(i + 1) & 3][j] * mod((3 * n - i + 2) * (3 * n - i + 1))));
    }
  }
  ll ans = 0;
  rep(j, 0, ofs) Add(ans, dp[(3 * n) & 3][j]);
  cout << mod(ans) << endl;

  return 0;
}