Problem p02974 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

constexpr int64 P = 1000000007;

// Verified: ABC133E, ABC132D, ABC130E
struct FiniteField {
private:
  int64 x;

public:
  FiniteField(int64 input_x) : x(input_x) {}
  FiniteField() : x(0) {}
  int64 Value() { return x; }
  inline FiniteField operator+(FiniteField o) {
    FiniteField r(*this);
    r += o;
    return r;
  }
  inline FiniteField operator-(FiniteField o) {
    FiniteField r(*this);
    r -= o;
    return r;
  }
  inline FiniteField operator*(FiniteField o) {
    FiniteField r(*this);
    r *= o;
    return r;
  }
  inline FiniteField operator/(FiniteField o) {
    FiniteField r(*this);
    r /= o;
    return r;
  }
  inline void operator+=(FiniteField o) { x = (x + o.x) % P; }
  inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; }
  inline void operator*=(FiniteField o) { x = (x * o.x) % P; }
  void operator/=(FiniteField o) {
    int64 p = P - 2;
    while (p) {
      if (p % 2) {
        *this *= o;
      }
      o *= o;
      p /= 2;
    }
  }
};

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

  int n, m;
  cin >> n >> m;
  vector<vector<vector<FiniteField>>> dp(
      n + 1, vector<vector<FiniteField>>(n + 1, vector<FiniteField>(m + 1)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= m; k++) {
        if (k + 2 * j <= m) {
          dp[i + 1][j][k + 2 * j] += dp[i][j][k];
        }
        if (k + 2 * (j + 1) <= m) {
          dp[i + 1][j + 1][k + 2 * (j + 1)] += dp[i][j][k];
        }
        if (j >= 1 && k + 2 * (j - 1) <= m) {
          dp[i + 1][j - 1][k + 2 * (j - 1)] += dp[i][j][k] * j * j;
        }
        if (j >= 1 && k + 2 * j <= m) {
          dp[i + 1][j][k + 2 * j] += dp[i][j][k] * 2 * j;
        }
      }
    }
  }

  cout << dp[n][0][m].Value() << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

constexpr int64 P = 1000000007;

// Verified: ABC133E, ABC132D, ABC130E
struct FiniteField {
private:
  int64 x;

public:
  FiniteField(int64 input_x) : x(input_x) {}
  FiniteField() : x(0) {}
  int64 Value() { return x; }
  inline FiniteField operator+(FiniteField o) {
    FiniteField r(*this);
    r += o;
    return r;
  }
  inline FiniteField operator-(FiniteField o) {
    FiniteField r(*this);
    r -= o;
    return r;
  }
  inline FiniteField operator*(FiniteField o) {
    FiniteField r(*this);
    r *= o;
    return r;
  }
  inline FiniteField operator/(FiniteField o) {
    FiniteField r(*this);
    r /= o;
    return r;
  }
  inline void operator+=(FiniteField o) { x = (x + o.x) % P; }
  inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; }
  inline void operator*=(FiniteField o) { x = (x * o.x) % P; }
  void operator/=(FiniteField o) {
    int64 p = P - 2;
    while (p) {
      if (p % 2) {
        *this *= o;
      }
      o *= o;
      p /= 2;
    }
  }
};

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

  int n, m;
  cin >> n >> m;
  vector<vector<vector<FiniteField>>> dp(
      n + 1, vector<vector<FiniteField>>(n + 1, vector<FiniteField>(m + 1)));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= m; k++) {
        if (dp[i][j][k].Value() == 0)
          continue;
        if (k + 2 * j <= m) {
          dp[i + 1][j][k + 2 * j] += dp[i][j][k];
        }
        if (k + 2 * (j + 1) <= m) {
          dp[i + 1][j + 1][k + 2 * (j + 1)] += dp[i][j][k];
        }
        if (j >= 1 && k + 2 * (j - 1) <= m) {
          dp[i + 1][j - 1][k + 2 * (j - 1)] += dp[i][j][k] * j * j;
        }
        if (j >= 1 && k + 2 * j <= m) {
          dp[i + 1][j][k + 2 * j] += dp[i][j][k] * 2 * j;
        }
      }
    }
  }

  cout << dp[n][0][m].Value() << endl;
}