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

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<double, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<double, ll> pdi;
typedef pair<double, pii> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
typedef complex<ld> point;
#define mod (ll)(998244353)
#define eps (1e-9)
#define oo (ll)(0x3f3f3f3f)
#define pi acos(-1)
#define MAXN (ll)(3e3 + 5)

ll dp[MAXN][MAXN];

ll qp(ll b, ll e) {
  if (!e)
    return 1;
  ll x = qp(b, e / 2);
  x = (x * x) % mod;
  if (e & 1)
    x = (x * b) % mod;
  return x;
}

ll prec[MAXN][MAXN];
ll preci[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  for (int i = 1; i < MAXN; i++) {
    prec[i][0] = 1;
    ll f1 = i;
    for (int e = 1; e < MAXN; e++)
      prec[i][e] = (prec[i][e - 1] * f1) % mod;
  }

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = qp(a, d - b);
  for (ll i = a + 1; i <= c; i++) {
    for (ll j = b + 1; j <= d; j++)
      dp[i - 1][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % mod;
    ;
    ll f1 = qp(i - 1, mod - 2);
    preci[0] = 1;
    for (int j = 1; j < MAXN; j++)
      preci[j] = (preci[j - 1] * f1) % mod;
    for (ll j = b + 1; j <= d; j++) {
      ll f1 = (dp[i - 1][j - 1] +
               j * ((dp[i - 1][j] - dp[i - 1][j - 1] + mod) % mod)) %
              mod;
      ll f2 = (prec[i][d - j] * preci[d - j]) % mod;
      dp[i][j] = (f1 * f2) % mod;
    }
    dp[i][b] = (b * dp[i - 1][b]) % mod;
    ll f2 = (prec[i][d - b] * preci[d - b]) % mod;
    dp[i][b] = (dp[i][b] * f2) % mod;
  }
  for (ll j = b + 1; j <= d; j++)
    dp[c][j] = (dp[c][j] + dp[c][j - 1]) % mod;
  //    for(int i=a;i<=c;i++,cout << '\\n')
  //        for(int j=b;j<=d;j++)
  //            cout << dp[i][j] << ' ';
  db(dp[c][d])

      return 0;
}
/**
2 1 3 4
31 41 59 265
**/


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

#include <bits/stdc++.h>
#define db(x) cout << (x) << '\\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<double, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<double, ll> pdi;
typedef pair<double, pii> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
typedef complex<ld> point;
#define mod (ll)(998244353)
#define eps (1e-9)
#define oo (ll)(0x3f3f3f3f)
#define pi acos(-1)
#define MAXN (ll)(3e3 + 5)

ll dp[MAXN][MAXN];

ll qp(ll b, ll e) {
  if (!e)
    return 1;
  ll x = qp(b, e / 2);
  x = (x * x) % mod;
  if (e & 1)
    x = (x * b) % mod;
  return x;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a][b] = qp(a, d - b);
  for (ll i = a + 1; i <= c; i++) {
    for (ll j = b + 1; j <= d; j++)
      dp[i - 1][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % mod;
    ;
    for (ll j = b + 1; j <= d; j++) {
      ll f1 = (dp[i - 1][j - 1] +
               j * ((dp[i - 1][j] - dp[i - 1][j - 1] + mod) % mod)) %
              mod;
      ll f2 = (qp(i, d - j) * qp(qp(i - 1, mod - 2), d - j)) % mod;
      dp[i][j] = (f1 * f2) % mod;
    }
    dp[i][b] = (b * dp[i - 1][b]) % mod;
    ll f2 = (qp(i, d - b) * qp(qp(i - 1, mod - 2), d - b)) % mod;
    dp[i][b] = (dp[i][b] * f2) % mod;
  }
  for (ll j = b + 1; j <= d; j++)
    dp[c][j] = (dp[c][j] + dp[c][j - 1]) % mod;
  //    for(int i=a;i<=c;i++,cout << '\\n')
  //        for(int j=b;j<=d;j++)
  //            cout << dp[i][j] << ' ';
  db(dp[c][d])

      return 0;
}
/**
2 1 3 4
**/