Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
// const ll mod = 1e9+7;
const ll mod = 998244353;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

/* modとる */
inline ll modu(ll a) { return (a % mod + mod) % mod; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  vector<ll> H(C - A);
  ll base = 0;
  vector<vector<ll>> pows(3010, vector<ll>(3010, 1));
  REPS(i, 3005) {
    REP(l, 3005) { pows[i][l + 1] = modu(pows[i][l] * i); }
  }
  REP(l, C - A) { H[l] = pows[B][l + 1]; }
  if (C == A) {
    cout << pows[A][D - B] << "\\n";
  } else {
    REP(i, D - B) {
      if (i == 0) {
        base = 1;
        RREP(l, C - A) {
          H[l] = modu(H[l] * (l + 1));
          RREP(k, l) {
            H[l] =
                H[l] + modu(modu(H[k] * (k + 1)) * pows[B + i + 1][l - k - 1]);
          }
        }
      } else {
        ll nmemory = 0;
        ll omemory = 0;
        REP(l, C - A) {
          H[l] = modu(H[l] * (l + 1 + A));
          nmemory = H[l];
          swap(nmemory, omemory);
          if (l > 0)
            H[l] = modu(H[l] + (B + i + 1) * H[l - 1] - nmemory * (B + i));
        }
        RREP(l, C - A) {
          H[l] = modu(H[l] +
                      base * modu((B + i) * modu(-pows[B + i][l + 1] +
                                                 (B + i) * pows[B + i + 1][l] +
                                                 pows[B + i + 1][l])));
        }
      }
      base = modu(base * A);
    }
    ll sum = modu(base * pows[D][C - A]);
    sum = modu(sum + *H.rbegin());
    cout << sum << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
// const ll mod = 1e9+7;
const ll mod = 998244353;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

/* modとる */
inline ll modu(ll a) { return (a % mod + mod) % mod; }

/* modつき二分累乗法 */
ll modpow(ll a, ll b) {
  ll res = 1;
  for (a = modu(a); b; a = modu(a * a), b >>= 1) {
    if (b & 1)
      res = modu(res * a);
  }
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  vector<ll> H(C - A);
  ll base = 1;
  REP(l, C - A) { H[l] = modpow(B, l + 1); }
  REP(i, D - B) {
    if (i == 0) {
      RREP(l, C - A) {
        H[l] = modu(H[l] * (l + 1));
        RREP(k, l) {
          H[l] = modu(
              H[l] + modu(modu(H[k] * (k + 1)) * modpow(B + i + 1, l - k - 1)));
        }
      }
    } else {
      RREP(l, C - A) {
        H[l] = modu(H[l] * (l + 1 + A));
        RREP(k, l) {
          H[l] = modu(
              H[l] + modu(modu(H[k] * (k + 2)) * modpow(B + i + 1, l - k - 1)));
        }
        REP(k, l + 1) {
          H[l] = modu(H[l] + modu(base * modu(modpow(B + i, l + 1 - k) *
                                              modpow(B + i + 1, k))));
        }
      }
    }
    base = modu(base * A);
  }
  ll sum = modu(base * modpow(D, C - A));
  sum = modu(sum + *H.rbegin());
  cout << sum << "\\n";
}