Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 998244353;
ll n, ans[N], dp[3005][3005], a, b, c, d;
ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  ///      cin >> ts;
  while (ts--) {
    cin >> a >> b >> c >> d;
    dp[a][b] = 1;
    rep(i, a, c + 1) {
      rep(j, b, d + 1) {
        dp[i][j] += (j)*dp[i - 1][j];
        dp[i][j] += (i)*dp[i][j - 1];
        dp[i][j] -= (i - 1) * (j - 1) * dp[i - 1][j - 1];
        if (dp[i][j] < 0) {
          dp[i][j] = (mod - llabs(dp[i][j]) % mod);
        }
        dp[i][j] %= mod;
      }
      /// cout << endl;
    }
    cout << dp[c][d] % mod << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 998244353;
ll n, ans[N], dp[3005][3005], a, b, c, d;
ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  ///      cin >> ts;
  while (ts--) {
    cin >> a >> b >> c >> d;
    dp[a][b] = 1;
    rep(i, a, c + 1) {
      rep(j, b, d + 1) {
        dp[i][j] += (j)*dp[i - 1][j];
        dp[i][j] += (i)*dp[i][j - 1];
        dp[i][j] -= (i - 1) * (j - 1) * dp[i - 1][j - 1];
        while (dp[i][j] < 0)
          dp[i][j] += mod;
        dp[i][j] %= mod;
      }
      /// cout << endl;
    }
    cout << dp[c][d] % mod << endl;
  }
}