Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 998244353;

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  vector<vector<ll>> dp(3001, vector<ll>(3001));
  dp[a][b] = 1;
  for (ll k = 1; k <= (c + d - a - b); k++) {
    for (ll i = a; i <= a + k; i++) {
      ll j = k - (i - a) + b;
      if (i > c || j > d)
        continue;
      if (i >= a + 1 && j >= b + 1) {
        dp[i][j] += dp[i - 1][j] * j % mod;
        dp[i][j] %= mod;
        dp[i][j] += dp[i][j - 1] * i % mod;
        dp[i][j] %= mod;
        ll add = ((dp[i - 1][j - 1] * (i - 1)) % mod) * (j - 1) % mod;
        dp[i][j] += mod - add;
        dp[i][j] %= mod;
      } else if (i >= a + 1) {
        dp[i][j] += dp[i - 1][j] * j % mod;
        dp[i][j] %= mod;
      } else if (j >= b + 1) {
        dp[i][j] += dp[i][j - 1] * i % mod;
        dp[i][j] %= mod;
      }

      // cout << i << " " << j << " " << dp[i][j] << endl;
    }
  }
  cout << dp[c][d] << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 998244353;

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  vector<vector<ll>> dp(3001, vector<ll>(3001)), ddp(3001, vector<ll>(3001));
  dp[a][b] = 1;
  for (ll k = 1; k <= (c + d - a - b); k++) {
    for (ll i = a; i <= a + k; i++) {
      ll j = k - (i - a) + b;
      if (i >= a + 1 && j >= b + 1) {
        dp[i][j] += dp[i - 1][j] * j % mod;
        dp[i][j] %= mod;
        dp[i][j] += dp[i][j - 1] * i % mod;
        dp[i][j] %= mod;
        ll add = ((dp[i - 1][j - 1] * (i - 1)) % mod) * (j - 1) % mod;
        dp[i][j] += mod - add;
        dp[i][j] %= mod;
      } else if (i >= a + 1) {
        dp[i][j] += dp[i - 1][j] * j % mod;
        dp[i][j] %= mod;
      } else if (j >= b + 1) {
        dp[i][j] += dp[i][j - 1] * i % mod;
        dp[i][j] %= mod;
      }

      // cout << i << " " << j << " " << dp[i][j] << endl;
    }
  }
  cout << dp[c][d] << endl;
}