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

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T> T power(T x, long long n, const T &m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x;
  T tmp = power(x, n / 2, m);
  if (n % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * x % m;
}

template <typename T> T inverse(T x, const T &m) { return power(x, m - 2, m); }

int A, B, C, D;

ll dp[3005][3005], sum[3005][3005];

ll POW[3005][3005], INV[3005][3005];

signed main() {
  cin >> A >> B >> C >> D;
  for (ll i = 1; i <= 3004; i++) {
    POW[i][0] = 1;
    INV[i][0] = inverse(power(i, 0, MOD), MOD);
    ll tmp = inverse(i, MOD);
    for (int j = 1; j <= 3004; j++) {
      POW[i][j] = POW[i][j - 1] * i % MOD;
      INV[i][j] = INV[i][j - 1] * tmp % MOD;
    }
  }

  for (int x = D; x >= B; x--) {
    for (int ymax = A; ymax <= C; ymax++) {
      if (x == D) {
        dp[x][ymax] = POW[x][C - ymax];
        continue;
      }
      dp[x][ymax] = dp[x + 1][ymax] * ymax % MOD;
      dp[x][ymax] +=
          (sum[x + 1][C] - sum[x + 1][ymax] + MOD) % MOD * INV[x][ymax];
      dp[x][ymax] %= MOD;
    }
    sum[x][A - 1] = 0;
    for (int ymax = A; ymax <= C; ymax++) {
      sum[x][ymax] = sum[x][ymax - 1] + dp[x][ymax] * POW[x - 1][ymax];
      sum[x][ymax] %= MOD;
    }
  }
  cout << dp[B][A] << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T> T power(T x, long long n, const T &m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x;
  T tmp = power(x, n / 2, m);
  if (n % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * x % m;
}

template <typename T> T inverse(T x, const T &m) { return power(x, m - 2, m); }

int A, B, C, D;

ll dp[3005][3005], sum[3005][3005];
ll solve(int x, int ymax) {
  if (x == D) {
    return power((ll)x, C - ymax, MOD);
  }
  if (dp[x][ymax] != -1)
    return dp[x][ymax];
  ll ret = 0;
  for (int i = 0; i < ymax; i++) {
    ret += solve(x + 1, ymax);
  }
  ret %= MOD;
  for (int i = ymax; i < C; i++) {
    ret += solve(x + 1, i + 1) * power((ll)x, i + 1, MOD) % MOD *
           inverse(power((ll)x, ymax, MOD), MOD);
    ret %= MOD;
  }
  return dp[x][ymax] = ret;
}

signed main() {
  cin >> A >> B >> C >> D;
  for (int x = D; x >= B; x--) {
    for (int ymax = A; ymax <= C; ymax++) {
      if (x == D) {
        dp[x][ymax] = power((ll)x, C - ymax, MOD);
        continue;
      }
      dp[x][ymax] = dp[x + 1][ymax] * ymax % MOD;
      dp[x][ymax] += (sum[x + 1][C] - sum[x + 1][ymax] + MOD) % MOD *
                     inverse(power((ll)x, ymax, MOD), MOD);
      dp[x][ymax] %= MOD;
    }
    sum[x][A - 1] = 0;
    for (int ymax = A; ymax <= C; ymax++) {
      sum[x][ymax] =
          sum[x][ymax - 1] + dp[x][ymax] * power((ll)x - 1, ymax, MOD);
      sum[x][ymax] %= MOD;
    }
  }
  // cout << solve(B, A) << endl;
  cout << dp[B][A] << endl;
}