alexjercan/bugnet huggingface dataset:
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
int gcd(int a, int b);
using namespace std;
long long int dp[3002][3002];
long long int mod = 998244353;
long long int DP(long long int X, long long int Y);
int main() {

  // B
  long long int A, B, C, D;
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  int i;
  for (i = A + 1; i <= C; i++) {
    dp[i][B] = (dp[i - 1][B] * B) % mod;
  }
  for (i = B + 1; i <= D; i++) {
    dp[A][i] = (dp[A][i - 1] * A) % mod;
  }
  cout << DP(C, D) << endl;
  return 0;

  // A
  /*int x;
  cin >> x;
  cout << 360 / gcd(360, x) << endl;

  return 0;
}
int gcd(int a, int b) {
  if (a == 0) {
          return b;
  }
  else if (b == 0) {
          return a;
  }
  else {
          return gcd(b, a % b);
  }*/
}
long long int DP(long long int X, long long int Y) {
  // cout << X << ':' << Y << endl;
  if (dp[X][Y] > 0) {
    return dp[X][Y];
  } else {
    // dp[X][Y] = (((DP(X - 1, Y)*Y)%mod) + ((DP(X, Y - 1)*X)%mod)-
    // ((DP(X-1,Y-1)*(X-1)*(Y-1))%mod)) % mod;
    dp[X][Y] = (((DP(X - 1, Y) * Y) % mod) + ((DP(X, Y - 1) * X) % mod) + mod -
                ((DP(X - 1, Y - 1) * (X - 1) * (Y - 1)) % mod)) %
               mod;

    return dp[X][Y];
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
int gcd(int a, int b);
using namespace std;
long long int dp[3002][3002];
long long int mod = 998244353;
long long int DP(long long int X, long long int Y);
int main() {

  // B
  long long int A, B, C, D;
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  int i;
  for (i = A + 1; i <= C; i++) {
    dp[i][B] = (dp[i - 1][B] * B) % mod;
  }
  for (i = B + 1; i <= D; i++) {
    dp[A][i] = (dp[A][i - 1] * A) % mod;
  }
  cout << DP(C, D) << endl;
  return 0;

  // A
  /*int x;
  cin >> x;
  cout << 360 / gcd(360, x) << endl;

  return 0;
}
int gcd(int a, int b) {
  if (a == 0) {
          return b;
  }
  else if (b == 0) {
          return a;
  }
  else {
          return gcd(b, a % b);
  }*/
}
long long int DP(long long int X, long long int Y) {
  // cout << X << ':' << Y << endl;
  if (dp[X][Y] > 0) {
    return dp[X][Y];
  } else {
    dp[X][Y] = (((DP(X - 1, Y) * Y) % mod) + ((DP(X, Y - 1) * X) % mod) -
                ((DP(X - 1, Y - 1) * (X - 1) * (Y - 1)) % mod)) %
               mod;
    return dp[X][Y];
  }
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  const int MAX = 3002;
  const int64_t MOD = 998244353;
  vector<vector<int64_t>> DP(MAX, vector<int64_t>(MAX, 0));
  DP[A][B] = 1;
  for (int h = A; h <= C; ++h) {
    for (int w = B; w <= D; ++w) {
      DP[h + 1][w] = (DP[h + 1][w] + (DP[h][w] * w) % MOD) % MOD;
      DP[h][w + 1] = (DP[h][w + 1] + (DP[h][w] * h) % MOD) % MOD;
      int64_t sub = (DP[h][w] * h * w) % MOD;
      DP[h + 1][w + 1] = ((DP[h + 1][w + 1] - sub) + MOD) % MOD;
    }
  }
  cout << DP[C][D] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  const int MAX = 3001;
  const int64_t MOD = 998244353;
  vector<vector<int64_t>> DP(MAX, vector<int64_t>(MAX, 0));
  DP[A][B] = 1;
  for (int h = A; h <= C; ++h) {
    for (int w = B; w <= D; ++w) {
      DP[h + 1][w] = (DP[h + 1][w] + (DP[h][w] * w) % MOD) % MOD;
      DP[h][w + 1] = (DP[h][w + 1] + (DP[h][w] * h) % MOD) % MOD;
      int64_t sub = (DP[h][w] * h * w) % MOD;
      DP[h + 1][w + 1] = ((DP[h + 1][w + 1] - sub) + MOD) % MOD;
    }
  }
  cout << DP[C][D] << endl;
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 998244353;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
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 long double PI = acos(-1.0);
// cout << fixed << setprecision(14);

int dp[3010][3010];

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int A, B, C, D;
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  for (int i = A; i <= C; i++) {
    for (int j = B; j <= D; j++) {
      if (i == A && j == B)
        continue;
      if (i == A) {
        dp[i][j] += dp[i][j - 1] * A;
      } else if (j == B) {
        dp[i][j] += dp[i - 1][j] * B;
      } else {
        dp[i][j] += dp[i][j - 1] * i + dp[i - 1][j] * j -
                    dp[i - 1][j - 1] * (i - 1) * (j - 1);
      }
      dp[i][j] += MOD * MOD;
      dp[i][j] %= MOD;
    }
  }
  cout << dp[C][D] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 998244353;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
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 long double PI = acos(-1.0);
// cout << fixed << setprecision(14);

int dp[3010][3010];

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int A, B, C, D;
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  for (int i = A; i <= C; i++) {
    for (int j = B; j <= D; j++) {
      if (i == A && j == B)
        continue;
      if (i == A) {
        dp[i][j] += dp[i][j - 1] * A;
      } else if (j == B) {
        dp[i][j] += dp[i - 1][j] * B;
      } else {
        dp[i][j] += dp[i][j - 1] * i + dp[i - 1][j] * j -
                    dp[i - 1][j - 1] * (i - 1) * (j - 1);
      }
      while (dp[i][j] <= 0)
        dp[i][j] += MOD;
      dp[i][j] %= MOD;
    }
  }
  cout << dp[C][D] << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1e9 + 7;
const int N = 3 * 1e3 + 111;
const int MOD = 998244353;
int dp[N][N];
signed main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (int i = a; i <= c; i++)
    for (int j = b; j <= d; j++) {
      dp[i + 1][j] += j * dp[i][j];
      dp[i][j + 1] += i * dp[i][j];
      dp[i + 1][j] %= MOD;
      dp[i][j + 1] %= MOD;
      dp[i + 1][j + 1] -= (dp[i][j] * (i) * (j) % MOD);
      dp[i + 1][j + 1] += MOD;
      dp[i + 1][j + 1] %= MOD;
    }
  cout << dp[c][d] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1e9 + 7;
const int N = 3 * 1e3 + 1;
const int MOD = 998244353;
int dp[N][N];
signed main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (int i = a; i <= c; i++)
    for (int j = b; j <= d; j++) {
      dp[i + 1][j] += j * dp[i][j];
      dp[i][j + 1] += i * dp[i][j];
      dp[i + 1][j] %= MOD;
      dp[i][j + 1] %= MOD;
      dp[i + 1][j + 1] -= (dp[i][j] * (i) * (j) % MOD);
      dp[i + 1][j + 1] += MOD;
      dp[i + 1][j + 1] %= MOD;
    }
  cout << dp[c][d] << '\\n';
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
ll mod = 998244353;
ll a, b, c, d;

ll memo[2][3005];

ll dp(ll x, ll y) {
  if (x == c && y == d)
    return memo[x][y] = 1;
  if (memo[x][y] != -1)
    return memo[x][y];
  if (y == d) {
    return memo[x][y] = (d * dp(x + 1, y)) % mod;
  }
  if (x == c) {
    return memo[x][y] = (c * dp(x, y + 1)) % mod;
  }
  ll ans = (x * dp(x, y + 1)) % mod + (y * dp(x + 1, y)) % mod;
  ans %= mod;
  ll minus = x * y;
  minus %= mod;
  minus *= dp(x + 1, y + 1);
  minus %= mod;
  return memo[x][y] = (ans - minus + mod) % mod;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> a >> b >> c >> d;
  memset(memo, -1, sizeof(memo));

  for (ll i = c; i >= a; i--) {
    for (ll j = d; j >= 0; j--) {
      memo[i % 2][j] = -1;
    }
    for (ll j = d; j >= b; j--) {
      ll x = i % 2;
      ll y = j;
      if (i == c && y == d) {
        memo[x][y] = 1;
        continue;
      }
      if (y == d) {
        memo[x][y] = (d * memo[1 - x][y]) % mod;
        continue;
      }
      if (i == c) {
        memo[x][y] = (c * memo[x][y + 1]) % mod;
        continue;
      }
      ll ans = (i * memo[x][y + 1]) % mod + (y * memo[1 - x][y]) % mod;
      ans %= mod;
      ll minus = i * y;
      minus %= mod;
      minus *= memo[1 - x][y + 1];
      minus %= mod;
      memo[x][y] = (ans - minus + mod) % mod;
    }
  }

  cout << memo[a % 2][b] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
ll mod = 998244353;
ll a, b, c, d;

ll memo[300][300];

ll dp(ll x, ll y) {
  if (x == c && y == d)
    return memo[x][y] = 1;
  if (memo[x][y] != -1)
    return memo[x][y];
  if (y == d) {
    return memo[x][y] = (d * dp(x + 1, y)) % mod;
  }
  if (x == c) {
    return memo[x][y] = (c * dp(x, y + 1)) % mod;
  }
  ll ans = (x * dp(x, y + 1)) % mod + (y * dp(x + 1, y)) % mod;
  ans %= mod;
  ll minus = x * y;
  minus %= mod;
  minus *= dp(x + 1, y + 1);
  minus %= mod;
  return memo[x][y] = (ans - minus + mod) % mod;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> a >> b >> c >> d;
  memset(memo, -1, sizeof(memo));
  cout << dp(a, b) << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long dp[4000][4000], a, b, c, d, mod = 998244353, x;
int main() {
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (long long i = a; i <= c; i++) {
    for (long long j = b; j <= d; j++) {
      if (a == i && b == j)
        continue;
      dp[i][j] = dp[i][j - 1] * i;
      dp[i][j] %= mod;
      dp[i][j] += dp[i - 1][j] * j;
      dp[i][j] %= mod;
      x = dp[i - 1][j - 1] * (i - 1);
      x %= mod;
      x *= (j - 1);
      x %= mod;
      dp[i][j] -= x;
      ;
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
  }
  cout << dp[c][d] % mod << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long dp[3000][3000], a, b, c, d, mod = 998244353;
int main() {
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (int i = a; i <= c; i++) {
    for (int j = b; j <= d; j++) {
      if (i == a && j == b)
        continue;
      dp[i][j] = dp[i][j - 1] * i + dp[i - 1][j] * j -
                 dp[i - 1][j - 1] * (i - 1) * (j - 1);
      dp[i][j] %= mod;
    }
  }
  cout << dp[c][d] % mod << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

/*
float like a butterfly
sting like a Bee

?#%^*&~`!  YOUR ONLY LIMIT IS YOUR MIND

*/

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_bsck
#define pop pop - back
#define S second
#define F first
#define q '\\n'
#define w .size()

using namespace std;

int main() {
  fast;
  long long int delta = 998244353, a, b, c, d, dp[4000][4000];
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (long long int i = b + 1; i <= 3020; i++) {
    dp[a][i] = dp[a][i - 1] * a;
    dp[a][i] %= delta;
  }
  for (long long int i = a + 1; i <= 3020; i++) {
    dp[i][b] = dp[i - 1][b] * b;
    dp[i][b] %= delta;
  }
  for (long long int i = a + 1; i <= 3020; i++) {
    for (long long int j = b + 1; j <= 3020; j++) {
      long long int x, y, z;
      x = (i * dp[i][j - 1]) % delta;
      y = (j * dp[i - 1][j]) % delta;
      z = (((i - 1) * (j - 1) * dp[i - 1][j - 1])) % delta;
      dp[i][j] = x + y;
      dp[i][j] += delta;
      dp[i][j] -= z;
      dp[i][j] %= delta;
    }
  }
  cout << dp[c][d];
}


Error: -11

Fixed submission:

/*
float like a butterfly
sting like a Bee

?#%^*&~`!  YOUR ONLY LIMIT IS YOUR MIND

*/

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_bsck
#define pop pop - back
#define S second
#define F first
#define q '\\n'
#define w .size()

using namespace std;

int main() {
  fast;
  long long int delta = 998244353, a, b, c, d, dp[4000][4000];
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  for (long long int i = 1; i <= 3020; i++) {
    dp[a][b + i] = dp[a][b + i - 1] * a;
    dp[a][b + i] %= delta;
  }
  for (long long int i = 1; i <= 3020; i++) {
    dp[a + i][b] = dp[a + i - 1][b] * b;
    dp[a + i][b] %= delta;
  }
  for (long long int i = a + 1; i < 3020; i++) {
    for (long long int j = b + 1; j < 3020; j++) {
      long long int x, y, z;
      x = (i * dp[i][j - 1]) % delta;
      y = (j * dp[i - 1][j]) % delta;
      z = (((i - 1) * (j - 1) * dp[i - 1][j - 1])) % delta;
      dp[i][j] = x + y;
      dp[i][j] += delta;
      dp[i][j] -= z;
      dp[i][j] %= delta;
    }
  }
  cout << dp[c][d];
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  ll dp[3010][3010] = {};
  dp[a][b] = 1;
  for (int i = a + 1; i <= c; i++) {
    dp[i][b] = dp[i - 1][b] * b % mod;
  }
  for (int i = b + 1; i <= d; i++) {
    dp[a][i] = dp[a][i - 1] * a % mod;
  }
  for (int i = a + 1; i <= c; i++) {
    for (int j = b + 1; j <= d; j++) {
      dp[i][j] = (dp[i][j - 1] * i % mod + dp[i - 1][j] * j % mod) % mod -
                 ((dp[i - 1][j - 1] * (i - 1)) % mod * (j - 1) % mod);
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
  }
  cout << dp[c][d] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int modpow(ll a, ll n, ll p) {
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * modpow(a, n - 1, p)) % p;
  if (!(n % 2)) {
    ll t = modpow(a, n / 2, p);
    return (t * t) % p;
  }
}

const int MAX = 1000000;
const int MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  COMinit();
  int n = (c - a) * (d - b);
  ll ans = 0;
  if (c - a + d - b > n) {
    ans += (modpow(b, c - a, mod) * modpow(a, d - b, mod)) % mod;
  }
  // cout << ans << endl;
  for (int i = 1; i <= d - b + c - a; i++) {
    for (int j = 0; j <= i; j++) {
      if (c - a - j >= 0 && d - b - (i - j) >= 0)
        ans += (ll)COM(n, i) *
               ((modpow(b, c - a - j, mod) * modpow(a, d - b - (i - j), mod)) %
                mod) %
               mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;

  long long ans_mat[c][d];
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < d; j++) {
      ans_mat[i][j] = 0;
    }
  }

  ans_mat[a - 1][b - 1] = 1;
  for (int i = a - 1; i < c; i++) {
    for (int j = b - 1; j < d; j++) {
      if (i != a - 1 || j != b - 1) {
        if (i == 0) {
          ans_mat[i][j] = 1;
        } else if (j == 0) {
          ans_mat[i][j] = 1;
        } else {
          ans_mat[i][j] = (ans_mat[i][j - 1] * (i + 1) % 998244353 +
                           ans_mat[i - 1][j] * (j + 1) % 998244353 -
                           ans_mat[i - 1][j - 1] * i % 998244353 * j) %
                          998244353;
          if (ans_mat[i][j] < 0) {
            ans_mat[i][j] += 998244353;
          }
        }
      }
    }
  }
  long ans = ans_mat[c - 1][d - 1] % 998244353;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int times(long x, int l) {
  long sum = 0;
  for (int i = 0; i < l; i++) {
    sum = (sum + x) % 998244353;
  }
  return sum;
}

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;

  long ans_mat[c][d];
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < d; j++) {
      ans_mat[i][j] = 0;
    }
  }

  ans_mat[a - 1][b - 1] = 1;
  for (int i = a - 1; i < c; i++) {
    for (int j = b - 1; j < d; j++) {
      if (i != a - 1 || j != b - 1) {
        if (i == 0) {
          ans_mat[i][j] = 1;
        } else if (j == 0) {
          ans_mat[i][j] = 1;
        } else {
          ans_mat[i][j] = (times(ans_mat[i][j - 1], i + 1) +
                           times(ans_mat[i - 1][j], j + 1) -
                           times(times(ans_mat[i - 1][j - 1], i), j)) %
                          998244353;
          if (ans_mat[i][j] < 0) {
            ans_mat[i][j] += 998244353;
          }
        }
      }
    }
  }
  cout << ans_mat[c - 1][d - 1] << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

ll A, B, C, D;
ll dp[3][3005][3005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> A >> B >> C >> D;
  if (A == C) {
    ll tmp = 1;
    for (int i = B + 1; i <= D; i++) {
      tmp *= A;
      tmp %= MOD;
    }
    cout << tmp << endl;
    return 0;
  }
  if (B == D) {
    ll tmp = 1;
    for (int i = A + 1; i <= C; i++) {
      tmp *= B;
      tmp %= MOD;
    }
    cout << tmp << endl;
    return 0;
  }
  dp[0][A][B] = 1;
  dp[1][A][B] = 1;
  for (int i = A + 1; i <= C; i++) {
    dp[0][i][B] = (dp[0][i - 1][B] * B) % MOD;
    dp[0][i][B] %= MOD;
  }
  for (int i = B + 1; i <= D; i++) {
    // for(int j = 0; j <= 2; j++){
    //     dp[min(2, j+1)][A][i] = dp[j][A][i-1]*A;
    //     dp[min(2, j+1)][A][i] %= MOD;
    // }
    dp[1][A][i] = (dp[1][A][i - 1] * A) % MOD;
    dp[1][A][i] %= MOD;
  }
  for (int i = A + 1; i <= C; i++) {
    for (int j = B + 1; j <= D; j++) {
      // cout << i << ' ' << j << endl;
      dp[0][i][j] += dp[0][i - 1][j];
      dp[0][i][j] %= MOD;

      dp[1][i][j] +=
          ((dp[0][i][j - 1] + dp[1][i][j - 1] + dp[2][i][j - 1]) * i) % MOD;
      dp[1][i][j] %= MOD;
      dp[1][i][j] += dp[0][i - 1][j];
      dp[1][i][j] %= MOD;

      dp[2][i][j] += dp[1][i - 1][j];
      dp[2][i][j] %= MOD;
      dp[2][i][j] += (dp[2][i - 1][j] * j) % MOD;
      dp[2][i][j] %= MOD;
    }
  }
  cout << (dp[0][C][D] + dp[1][C][D] + dp[2][C][D]) % MOD << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

ll A, B, C, D;
ll dp[3][3005][3005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> A >> B >> C >> D;
  dp[0][A][B] = 1;
  dp[1][A][B] = 1;
  for (int i = A + 1; i <= C; i++) {
    dp[0][i][B] = (dp[0][i - 1][B] * B) % MOD;
    dp[0][i][B] %= MOD;
  }
  for (int i = B + 1; i <= D; i++) {
    // for(int j = 0; j <= 2; j++){
    //     dp[min(2, j+1)][A][i] = dp[j][A][i-1]*A;
    //     dp[min(2, j+1)][A][i] %= MOD;
    // }
    dp[1][A][i] = (dp[1][A][i - 1] * A) % MOD;
    dp[1][A][i] %= MOD;
  }
  for (int i = A + 1; i <= C; i++) {
    for (int j = B + 1; j <= D; j++) {
      // cout << i << ' ' << j << endl;
      dp[0][i][j] += dp[0][i - 1][j];
      dp[0][i][j] %= MOD;

      dp[1][i][j] +=
          ((dp[0][i][j - 1] + dp[1][i][j - 1] + dp[2][i][j - 1]) * i) % MOD;
      dp[1][i][j] %= MOD;
      dp[1][i][j] += dp[0][i - 1][j];
      dp[1][i][j] %= MOD;

      dp[2][i][j] += dp[1][i - 1][j];
      dp[2][i][j] %= MOD;
      dp[2][i][j] += (dp[2][i - 1][j] * j) % MOD;
      dp[2][i][j] %= MOD;
    }
  }
  cout << (dp[0][C][D] + dp[1][C][D] + dp[2][C][D]) % MOD << endl;
  if (A == C)
    return -1;
  if (B == D)
    return -1;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll DP[3005][3005];

int main() {
  ll mod1 = 998244353;
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  DP[A][B] = 1;
  for (ll i = A; i <= C; ++i) {
    for (ll j = B; j <= D; ++j) {
      if (i == A && j == B)
        continue;
      if (i > A && j > B) {
        DP[i][j] = DP[i - 1][j] * j % mod1 + DP[i][j - 1] * i % mod1 -
                   (DP[i - 1][j - 1] * (i - 1) * (j - 1) % mod1);
      } else {
        DP[i][j] = DP[i - 1][j] * j % mod1 + DP[i][j - 1] * i % mod1;
      }
      while (DP[i][j] < 0)
        DP[i][j] += mod1;
      DP[i][j] %= mod1;
    }
  }
  cout << DP[C][D] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll DP[3005][3005];

int main() {
  ll mod1 = 998244353;
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  DP[A][B] = 1;
  for (ll i = A; i <= C; ++i) {
    for (ll j = B; j <= D; ++j) {
      if (i == A && j == B)
        continue;
      if (i > A && j > B) {
        DP[i][j] = DP[i - 1][j] * j + DP[i][j - 1] * i -
                   DP[i - 1][j - 1] * (i - 1) * (j - 1);
      } else {
        DP[i][j] = DP[i - 1][j] * j + DP[i][j - 1] * i;
      }
      while (DP[i][j] < 0)
        DP[i][j] += mod1;
      DP[i][j] %= mod1;
    }
  }
  cout << DP[C][D] << endl;
}