Problem p02974 (C++)

Buggy submission (status: Runtime Error):
#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 mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E17;

#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);
///////////////////////////////////////

int main() {
  ll m, k;
  cin >> m >> k;
  if (k % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll N = 2 * m;
  ll dp[m + 1][N + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  rep(n, 0, m) {
    rep(d, 0, N) {
      rep(l, 0, m) {
        ll ad1 = 0;
        rep(li, l, n / 2) {
          if (n - 1 >= 0 && d - l >= 0)
            Add(ad1, (dp[n - 1][d - l][li]));
        }
        Add(dp[n][d][l], mod(ad1 * (2 * l + 1)));
        ll ad2 = 0;
        rep(li, l - 1, n / 2) {
          if (n - 2 >= 0 && (d - (2 * l - 1)) >= 0 && li >= 0)
            Add(ad2, dp[n - 2][d - (2 * l - 1)][li]);
        }
        Add(dp[n][d][l], mod(l * l * ad2));
      }
    }
  }
  /*rep(i,0,N){
      rep(j,0,m){
          cout<<dp[m][i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, m) { Add(ans, dp[m][k / 2][i]); }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#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 mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E17;

#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);
///////////////////////////////////////

int main() {
  ll m, k;
  cin >> m >> k;
  if (k % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll N = k / 2;
  ll dp[m + 1][N + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  rep(n, 0, m) {
    rep(d, 0, N) {
      rep(l, 0, m) {
        ll ad1 = 0;
        rep(li, l, n / 2) {
          if (n - 1 >= 0 && d - l >= 0)
            Add(ad1, (dp[n - 1][d - l][li]));
        }
        Add(dp[n][d][l], mod(ad1 * (2 * l + 1)));
        ll ad2 = 0;
        rep(li, l - 1, n / 2) {
          if (n - 2 >= 0 && (d - (2 * l - 1)) >= 0 && li >= 0)
            Add(ad2, dp[n - 2][d - (2 * l - 1)][li]);
        }
        Add(dp[n][d][l], mod(l * l * ad2));
      }
    }
  }
  /*rep(i,0,N){
      rep(j,0,m){
          cout<<dp[m][i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, m) { Add(ans, dp[m][k / 2][i]); }
  cout << ans << endl;

  return 0;
}