Problem p02634 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using ordered_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) (*find_by_order(i))
// #define ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
using cd = complex<double>;
const double pi = acos(-1);
#define mod 998244353
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/

int dp[3001][3001];
void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  rep(i, a, c + 1) {
    rep(j, b, d + 1) {
      dp[i][j] -= (dp[i - 1][j - 1] * (i - 1) * (j - 1)) % mod;
      if (dp[i][j] < 0)
        dp[i][j] += mod;
      dp[i + 1][j] += (dp[i][j] * j) % mod;
      if (dp[i + 1][j] >= mod)
        dp[i + 1][j] -= mod;
      dp[i][j + 1] += (dp[i][j] * i) % mod;
      if (dp[i][j + 1] >= mod)
        dp[i][j + 1] -= mod;
    }
  }
  cout << dp[c][d];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}

Fixed submission:
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using ordered_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) (*find_by_order(i))
// #define ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
using cd = complex<double>;
const double pi = acos(-1);
#define mod 998244353
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/

int dp[3005][3005];
void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  rep(i, a, c + 1) {
    rep(j, b, d + 1) {
      dp[i][j] -= (dp[i - 1][j - 1] * (i - 1) * (j - 1)) % mod;
      if (dp[i][j] < 0)
        dp[i][j] += mod;
      dp[i + 1][j] += (dp[i][j] * j) % mod;
      if (dp[i + 1][j] >= mod)
        dp[i + 1][j] -= mod;
      dp[i][j + 1] += (dp[i][j] * i) % mod;
      if (dp[i][j + 1] >= mod)
        dp[i][j + 1] -= mod;
    }
  }
  cout << dp[c][d];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}