alexjercan/bugnet huggingface dataset:
Problem p02633 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>

using namespace std;
/*
void calculate(float X) {
  float deg = X;
  float T = 3.14159265/180;
  int t = 0;
  float x = 0, y = 0;
  float e = 0.001;
  while (true) {
    t++;
    x -= sin(deg*T);
    y -= cos(deg*T);
    deg += X;
    //cout << X << endl;
    //cout << x << " " << y << endl;
    if (-1*e <= x && x <= e && -1*e <= y && y <= e) {
      break;
    }
  }
  cout << t << endl;
}
*/

int main() {
  int ans[179] = {
      360, 180, 120, 90,  72, 60,  360, 45,  40,  36, 360, 30,  360, 180, 24,
      45,  360, 20,  360, 18, 120, 180, 360, 15,  72, 180, 40,  90,  360, 12,
      360, 45,  120, 180, 72, 10,  360, 180, 120, 9,  360, 60,  360, 90,  8,
      180, 360, 15,  360, 36, 120, 90,  360, 20,  72, 45,  120, 180, 360, 6,
      360, 180, 40,  45,  72, 60,  360, 90,  120, 36, 360, 5,   360, 180, 24,
      90,  360, 60,  360, 9,  40,  180, 360, 30,  72, 180, 120, 45,  360, 4,
      360, 90,  120, 180, 72, 15,  360, 180, 40,  18, 360, 60,  360, 45,  24,
      180, 360, 10,  360, 36, 120, 45,  360, 60,  72, 90,  40,  180, 360, 3,
      360, 180, 120, 90,  72, 20,  360, 45,  120, 36, 360, 30,  360, 180, 8,
      45,  360, 60,  360, 18, 120, 180, 360, 5,   72, 180, 120, 90,  360, 12,
      360, 45,  40,  180, 72, 30,  360, 180, 120, 9,  360, 20,  360, 90,  24,
      180, 360, 15,  360, 36, 40,  90,  360, 60,  72, 45,  120, 180, 360,
  };
  int X;
  cin >> X;
  cout << ans[X - 1] << endl;
}


Error: 4

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;
/*
void calculate(float X) {
  float deg = X;
  float T = 3.14159265/180;
  int t = 0;
  float x = 0, y = 0;
  float e = 0.001;
  while (true) {
    t++;
    x -= sin(deg*T);
    y -= cos(deg*T);
    deg += X;
    //cout << X << endl;
    //cout << x << " " << y << endl;
    if (-1*e <= x && x <= e && -1*e <= y && y <= e) {
      break;
    }
  }
  cout << t << endl;
}
*/

int main() {
  int ans[179] = {
      360, 180, 120, 90,  72, 60,  360, 45,  40,  36, 360, 30,  360, 180, 24,
      45,  360, 20,  360, 18, 120, 180, 360, 15,  72, 180, 40,  90,  360, 12,
      360, 45,  120, 180, 72, 10,  360, 180, 120, 9,  360, 60,  360, 90,  8,
      180, 360, 15,  360, 36, 120, 90,  360, 20,  72, 45,  120, 180, 360, 6,
      360, 180, 40,  45,  72, 60,  360, 90,  120, 36, 360, 5,   360, 180, 24,
      90,  360, 60,  360, 9,  40,  180, 360, 30,  72, 180, 120, 45,  360, 4,
      360, 90,  120, 180, 72, 15,  360, 180, 40,  18, 360, 60,  360, 45,  24,
      180, 360, 10,  360, 36, 120, 45,  360, 60,  72, 90,  40,  180, 360, 3,
      360, 180, 120, 90,  72, 20,  360, 45,  120, 36, 360, 30,  360, 180, 8,
      45,  360, 60,  360, 18, 120, 180, 360, 5,   72, 180, 120, 90,  360, 12,
      360, 45,  40,  180, 72, 30,  360, 180, 120, 9,  360, 20,  360, 90,  24,
      180, 360, 15,  360, 36, 40,  90,  360, 60,  72, 45,  120, 180, 360,
  };
  int X;
  cin >> X;
  return ans[X - 1];
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define all(n) n.begin(), n.end()
#define ll long long

#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll maxn = 3e3 + 120;
const ll maxx = 1e16;
const ll mod1 = 1e9 + 7;
const ll mod = 998244353;
vector<ll> adj[maxn];
ll dp[maxn][maxn];
ll n, m;
ll mu(ll a, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  ll t = mu(a, n / 2, mod);
  if (n % 2 == 0) {
    return (t % mod * t % mod) % mod;
  }
  return (a % mod * t % mod * t % mod) % mod;
}
ll f(ll n1, ll m1) {
  if (n1 == n && m1 == m)
    return 1;
  if (dp[n1][m1] != -1)
    return dp[n1][m1];
  ll ans;
  if (n1 == n) {
    ans = (f(n1, m1 + 1) % mod * n1 % mod) % mod;
  } else if (m1 == m) {
    ans = (f(n1 + 1, m1) % mod * m1 % mod) % mod;
  } else {
    ans = (n1 * f(n1, m1 + 1) + m1 * f(n1 + 1, m1) -
           n1 * m1 * f(n1 + 1, m1 + 1) + mod * mod) %
          mod;
    //  dp[n1][m1]=((((f(n1+1,m1)%mod *(m1)%mod  )%mod+(f(n1,m1+1)%mod *(n1)%mod
    //  )%mod-n1%mod *m1%mod *f(n1+1,m1+1)%mod +mod *mod )))%mod;
  }
  return dp[n1][m1] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("TEAMS.INP", "r")) {
    freopen("TEAMS.INP", "r", stdin);
    freopen("TEAMS.OUT", "w", stdout);
  }
  ll a, b;
  cin >> a >> b >> n >> m;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      dp[i][j] = -1;
    }
  }
  cout << f(a, b);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define all(n) n.begin(), n.end()
#define ll long long

#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define ff first
#define ss second

using namespace std;
const ll maxn = 1e3 + 120;
const ll maxx = 1e16;
const ll mod1 = 1e9 + 7;
const ll mod = 998244353;
vector<ll> adj[maxn];
ll dp[maxn][maxn];
ll n, m;
ll mu(ll a, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  ll t = mu(a, n / 2, mod);
  if (n % 2 == 0) {
    return (t % mod * t % mod) % mod;
  }
  return (a % mod * t % mod * t % mod) % mod;
}
ll f(ll n1, ll m1) {
  if (n1 == n && m1 == m)
    return 1;
  if (dp[n1][m1] != -1)
    return dp[n1][m1];
  if (n1 == n) {
    dp[n1][m1] = (f(n1, m1 + 1) % mod * n1 % mod) % mod;
  } else if (m1 == m) {
    dp[n1][m1] = (f(n1 + 1, m1) % mod * m1 % mod) % mod;
  } else {
    dp[n1][m1] =
        ((((f(n1 + 1, m1) % mod * (m1) % mod) % mod +
           (f(n1, m1 + 1) % mod * (n1) % mod) % mod -
           n1 % mod * m1 % mod * f(n1 + 1, m1 + 1) % mod + mod * mod))) %
        mod;
  }
  return dp[n1][m1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("TEAMS.INP", "r")) {
    freopen("TEAMS.INP", "r", stdin);
    freopen("TEAMS.OUT", "w", stdout);
  }
  ll a, b;
  cin >> a >> b >> n >> m;
  memset(dp, -1, sizeof(dp));
  cout << f(a, b);
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  ll dp[3100][3100];
  ll mod = 998244353;
  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 > 0 && j - b > 0) {
        dp[i][j] = (dp[i - 1][j] * j) % mod + (dp[i][j - 1] * i) % mod;
        dp[i][j] %= mod;
        dp[i][j] -= ((dp[i - 1][j - 1] * (i - 1)) % mod * (j - 1)) % mod;
        while (dp[i][j] < 0) {
          dp[i][j] += mod;
        }
        dp[i][j] += mod;
        dp[i][j] %= mod;
        continue;
      }
      if (i - a == 0) {
        dp[i][j] = dp[i][j - 1] * i;
        dp[i][j] %= mod;
      }
      if (j - b == 0) {
        dp[i][j] = dp[i - 1][j] * j;
        dp[i][j] %= mod;
      }
    }
  }
  cout << dp[c][d] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  ll dp[3100][3100];
  ll mod = 998244353;
  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 > 0 && j - b > 0) {
        dp[i][j] = (dp[i - 1][j] * j) % mod + (dp[i][j - 1] * i) % mod -
                   (dp[i - 1][j - 1] * (i - 1) * (j - 1));
        while (dp[i][j] < 0) {
          dp[i][j] += mod;
        }
        dp[i][j] %= mod;
        continue;
      }
      if (i - a == 0) {
        dp[i][j] = dp[i][j - 1] * i;
        dp[i][j] %= mod;
      }
      if (j - b == 0) {
        dp[i][j] = dp[i - 1][j] * j;
        dp[i][j] %= mod;
      }
    }
  }
  cout << dp[c][d] << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second
#define vec vector<ll>
#define mat vector<vector<ll>>
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 998244353;
const ll mod = 998244353;
vector<vector<ll>> dp(3010, vector<ll>(3010, -inf));
ll a, b, c, d;
ll cnt;
ll solve(ll i, ll j) {
  if (dp[i][j] >= 0)
    return dp[i][j];
  cnt++;
  ll res = 0;
  if (i > a) {
    res = (res + solve(i - 1, j) * j) % mod;
  }
  if (j > b) {
    res = (res + solve(i, j - 1) * i) % mod;
  }
  if (i > a && j > b) {
    res = (res - (i - 1) * (j - 1) % mod * dp[i - 1][j - 1]) % mod;
  }
  res %= inf;
  if (res < 0)
    res += inf;
  dp[i][j] = res;
  return res;
}
signed main() {
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  cnt = 0;
  ll ans = solve(c, d);
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second
#define vec vector<ll>
#define mat vector<vector<ll>>
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
vector<vector<ll>> dp(3010, vector<ll>(3010, -1));
ll a, b, c, d;
ll solve(ll i, ll j) {
  if (dp[i][j] >= 0)
    return dp[i][j];
  ll res = 0;
  if (i > a) {
    res = (res + solve(i - 1, j) * j) % mod;
  }
  if (j > b) {
    res = (res + solve(i, j - 1) * i) % mod;
  }
  if (i > a && j > b) {
    res = (res - (i - 1) * (j - 1) % mod * dp[i - 1][j - 1]) % mod;
  }
  dp[i][j] = res;
  return res;
}
signed main() {
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  ll ans = solve(c, d);
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

long long dp[3004][3004];

int main() {
  int A, B, C, D;
  scanf("%d", &A);
  scanf("%d", &B);
  scanf("%d", &C);
  scanf("%d", &D);
  int H = C - A;
  int W = D - B;
  long long mod = 998244353;
  dp[0][0] = 1;
  for (int i = 0; i <= H + W; i++) {
    for (int h = 0; h <= H; h++) {
      int w = i - h;
      if (w < 0 || w > W)
        continue;
      // delete
      if (h > 0 && w > 0) {
        dp[h][w] -= dp[h - 1][w - 1] * (A + h - 1) * (B + w - 1);
        while (dp[h][w] < 0) {
          dp[h][w] += mod;
        }
      }
      // printf("dp[%d][%d]=%lld\\n", h, w, dp[h][w]);
      //  add
      if (h + 1 <= H) {
        dp[h + 1][w] += dp[h][w] * (B + w);
        dp[h + 1][w] %= mod;
      }
      if (w + 1 <= W) {
        dp[h][w + 1] += dp[h][w] * (A + h);
        dp[h][w + 1] %= mod;
      }
    }
  }
  std::cout << dp[H][W] << std::endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

long long dp[3004][3004];

int main() {
  int A, B, C, D;
  scanf("%d", &A);
  scanf("%d", &B);
  scanf("%d", &C);
  scanf("%d", &D);
  int H = C - A;
  int W = D - B;
  long long mod = 998244353;
  dp[0][0] = 1;
  for (int i = 0; i <= H + W; i++) {
    for (int h = 0; h <= i; h++) {
      int w = i - h;
      if (w < 0 || w >= W)
        continue;
      // delete
      if (h > 0 && w > 0) {
        dp[h][w] -= dp[h - 1][w - 1] * (A + h - 1) * (B + w - 1);
        while (dp[h][w] < 0) {
          dp[h][w] += mod;
        }
      }
      // printf("dp[%d][%d]=%lld\\n", h, w, dp[h][w]);
      //  add
      dp[h + 1][w] += dp[h][w] * (B + w);
      dp[h + 1][w] %= mod;
      dp[h][w + 1] += dp[h][w] * (A + h);
      dp[h][w + 1] %= mod;
    }
  }
  std::cout << dp[H][W] << std::endl;
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 998244353, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll a, b, c, d;
ll dp[3001][3001];

ll solve(ll h, ll w) {
  if (dp[h][w] != -1LL)
    return dp[h][w];
  if (h == a && w == b) {
    return 1LL;
  }
  if (h == a) {
    return dp[h][w] = modpow(h, w - b);
  }
  if (w == b) {
    return dp[h][w] = modpow(w, h - a);
  }
  return dp[h][w] =
             (((solve(h, w - 1) * h) % mod + (solve(h - 1, w) * w) % mod) %
                  mod -
              ((solve(h - 1, w - 1) * (h - 1)) % mod * (w - 1))) %
             mod;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  memset(dp, -1, sizeof(dp));
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  cout << (solve(c, d) + mod) % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 998244353, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

ll a, b, c, d;
ll dp[3000][3000];

ll solve(ll h, ll w) {
  if (dp[h][w] != -1)
    return dp[h][w];
  if (h == a && w == b) {
    return 1LL;
  }
  if (h == a) {
    return dp[h][w] = solve(h, w - 1) * h % mod;
  }
  if (w == b) {
    return dp[h][w] = solve(h - 1, w) * w % mod;
  }
  return dp[h][w] = ((solve(h - 1, w) * h + solve(h - 1, w) * w) % mod -
                     ((solve(h - 1, w - 1) * (h - 1)) % mod * (w - 1)) % mod) %
                    mod;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  memset(dp, -1, sizeof(dp));
  cin >> a >> b >> c >> d;
  dp[a][b] = 1;
  cout << solve(c, d) << endl;
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll mod = 998244353;
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  if (A == C and B == D)
    cout << 1 << endl;
  else if (A == C) {
    ll ans = 1;
    rep(i, D - B) ans = ans * C % mod;
    cout << ans << endl;
  } else if (B == D) {
    ll ans = 1;
    rep(i, C - A) ans = ans * D % mod;
    cout << ans << endl;
  } else {
    VVL X(C - A + 1, VL(D - B + 1));
    X[0][0] = 1;
    rep(i, C - A + 1) {
      rep(j, D - B + 1) {
        if (i)
          (X[i][j] += X[i - 1][j] * (j + B) % mod) %= mod;
        if (j)
          (X[i][j] += X[i][j - 1] * (i + A) % mod) %= mod;
        if (i and j)
          (X[i][j] += mod - X[i - 1][j - 1] * (i + A - 1) % mod * (j + B - 1) %
                                mod) %= mod;
        if (X[i][j] < 0)
          X[i][j] += mod;
      }
    }
    cout << X[C - A][D - B] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

ll mod = 998244353;
ll A, B, C, D;
map<PLL, ll> M;

ll solve(ll c, ll d) {
  if (M.find(mp(c, d)) != M.end())
    return M[mp(c, d)];
  if (c == A)
    return M[mp(c, d)] = solve(c, d - 1) * c % mod;
  if (d == B)
    return M[mp(c, d)] = solve(c - 1, d) * d % mod;
  ll tmp = solve(c, d - 1) * c % mod;
  tmp = (tmp + solve(c - 1, d) * d) % mod;
  tmp = (tmp + mod - solve(c - 1, d - 1) * (c - 1) % mod * (d - 1) % mod) % mod;
  return M[mp(c, d)] = tmp;
}

int main() {
  cin >> A >> B >> C >> D;
  M[mp(A, B)] = 1;
  cout << solve(C, D) << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 998244353;
const int N = 3e3 + 5;
using namespace std;
int dp[N][N][2];
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  if (C > A)
    dp[C][D][0] = 1;
  if (D > B)
    dp[C][D][1] = 1;

  ll ans = 0;
  for (int i = C; i >= A; i--)
    for (int j = D; j >= B; j--) {
      dp[i][j - 1][0] = (dp[i][j - 1][0] + (ll)dp[i][j][1] * i) % mod;
      dp[i][j - 1][1] = (dp[i][j - 1][1] + (ll)dp[i][j][1] * i) % mod;
      dp[i - 1][j][0] = (dp[i - 1][j][0] + (ll)dp[i][j][0] * j) % mod;
      dp[i - 1][j][1] = (dp[i - 1][j][1] + (ll)dp[i][j][0]) % mod;

      if (i == A && j - 1 == B)
        ans = (ans + (ll)dp[i][j][1] * i) % mod;
      if (i - 1 == A && j == B)
        ans = (ans + (ll)dp[i][j][0] * j) % mod;
    }
  if (A == C && B == D)
    ans = 1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 998244353;
const int N = 1e3 + 5;
using namespace std;
int dp[N][N][2];
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  if (C > A)
    dp[C][D][0] = 1;
  if (D > B)
    dp[C][D][1] = 1;

  ll ans = 0;
  for (int i = C; i >= A; i--)
    for (int j = D; j >= B; j--) {
      dp[i][j - 1][0] = (dp[i][j - 1][0] + (ll)dp[i][j][1] * i) % mod;
      dp[i][j - 1][1] = (dp[i][j - 1][1] + (ll)dp[i][j][1] * i) % mod;
      dp[i - 1][j][0] = (dp[i - 1][j][0] + (ll)dp[i][j][0] * j) % mod;
      dp[i - 1][j][1] = (dp[i - 1][j][1] + (ll)dp[i][j][0]) % mod;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 998244353;
typedef long long LL;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  vector<vector<LL>> dp(C + 1, vector<LL>(D + 1, 0));
  dp[C][D] = 1;
  for (int c = D - 1; c >= B; --c) {
    dp[C][c] = C * dp[C][c + 1];
    dp[C][c] %= kMod;
  }
  for (LL r = C - 1; r >= A; --r) {
    LL S = 0;
    dp[r][D] = (D * dp[r + 1][D]) % kMod;
    for (LL c = D - 1; c >= B; --c) {
      S = (r * (S + dp[r + 1][c + 1])) % kMod;
      dp[r][c] = (c * dp[r + 1][c] + S) % kMod;
    }
  }
  cout << dp[A][B] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 998244353;
typedef long long LL;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  vector<vector<LL>> dp(C + 1, vector<LL>(D + 1, 0));
  dp[C][D] = 1;
  for (int c = D - 1; c >= B; --c) {
    dp[C][c] = C * dp[C][c + 1];
    dp[C][c] %= kMod;
  }
  for (LL r = C - 1; r >= A; --r) {
    for (LL c = B; c <= D; ++c) {
      dp[r][c] = c * dp[r + 1][c];
      dp[r][c] %= kMod;
      LL k = 1;
      for (int i = c + 1; i <= D; ++i) {
        k = (k * r) % kMod;
        dp[r][c] += k * dp[r + 1][i];
        dp[r][c] %= kMod;
      }
      //      cout << "dp[" << r << "][" << c << "] = " << dp[r][c] << endl;
    }
  }
  cout << dp[A][B] << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 998244353;
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
ll f[3005][3005];
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  f[a][b] = 1;
  for (int i = a; i <= c; i++) {
    for (int j = b; j <= d; j++) {
      if (i > a)
        f[i][j] += f[i - 1][j] * j;
      if (j > b)
        f[i][j] += f[i][j - 1] * i;
      if (i > a && j > b)
        f[i][j] -= (ll)(i - 1) * (j - 1) * f[i - 1][j - 1];
      f[i][j] %= mod;
    }
  }
  cout << (f[c][d] % mod + mod) % mod;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 998244353;
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
ll f[2005][2005];
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  f[a][b] = 1;
  for (int i = a; i <= c; i++) {
    for (int j = b; j <= d; j++) {
      if (i > a)
        f[i][j] += f[i - 1][j] * j;
      if (j > b)
        f[i][j] += f[i][j - 1] * i;
      if (i > a && j > b)
        f[i][j] -= (ll)(i - 1) * (j - 1) * f[i - 1][j - 1];
      f[i][j] %= mod;
    }
  }
  cout << (f[c][d] % mod + mod) % mod;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
long long dp[3001][3001];
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  for (int i = A; i <= C; i++) {
    for (int j = B; j <= D; j++) {
      if (i == A && j == B)
        dp[A][B] = 1;
      else if (i == A && j != B)
        dp[A][j] = (A * dp[A][j - 1]) % 998244353;
      else if (i != A && j == B)
        dp[i][B] = (B * dp[i - 1][B]) % 998244353;
      else {
        dp[i][j] =
            (i * dp[i][j - 1] + j * dp[i - 1][j] -
             ((i - 1) * (j - 1) * (dp[i - 1][j - 1] % 998244353)) % 998244353);
        if (dp[i][j] < 0)
          dp[i][j] += 998244353;
        dp[i][j] = dp[i][j] % 998244353;
      }
    }
  }
  cout << dp[C][D] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  long long dp[3001][3001];
  for (int i = A; i <= C; i++) {
    for (int j = B; j <= D; j++) {
      if (i == A && j == B)
        dp[A][B] = 1;
      else if (i == A && j != B)
        dp[A][j] = (A * dp[A][j - 1]) % 998244353;
      else if (i != A && j == B)
        dp[i][B] = (B * dp[i - 1][B]) % 998244353;
      else {
        dp[i][j] = (i * dp[i][j - 1] + j * dp[i - 1][j] -
                    (i - 1) * (j - 1) * dp[i - 1][j - 1]);
        while (dp[i][j] < 0)
          dp[i][j] += 998244353;
        dp[i][j] = dp[i][j] % 998244353;
      }
    }
  }
  cout << dp[C][D] << endl;

  return 0;
}