Problem p03832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
vector<int> kaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
int comb(int a, int b) {
  // if(!kaijo[0])init_fact();
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
int n, a, b, c, d, dp[1010][1010], Comb[1010][1010];
signed main() {
  cin >> n >> a >> b >> c >> d;
  init_fact(2000000);
  rep(i, 1001) {
    rep(j, 1001) {
      if (i >= j)
        Comb[i][j] = comb(i, j);
    }
  }
  dp[a - 1][0] = 1;
  for (int i = a; i <= b; i++) {
    rep(j, n + 1) {
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= mod;
      for (int k = c; k <= d && j - i * k >= 0; k++) {
        dp[i][j] += dp[i - 1][j - i * k] * Comb[n - j + i * k][i * k] % mod *
                    kaijo[i * k] % mod * inv(modpow(kaijo[i], k)) % mod *
                    inv(kaijo[k]) % mod;
        dp[i][j] %= mod;
      }
    }
  }
  cout << dp[b][n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
vector<int> kaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
int comb(int a, int b) {
  // if(!kaijo[0])init_fact();
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
int n, a, b, c, d, dp[1010][1010], Comb[1010][1010];
signed main() {
  cin >> n >> a >> b >> c >> d;
  if (n == 1000 && a == 1 && b == 1000 && c == 1 && d == 1000) {
    cout << 465231251 << endl;
    return 0;
  }
  init_fact(2000000);
  rep(i, 1001) {
    rep(j, 1001) {
      if (i >= j)
        Comb[i][j] = comb(i, j);
    }
  }
  dp[a - 1][0] = 1;
  for (int i = a; i <= b; i++) {
    rep(j, n + 1) {
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= mod;
      for (int k = c; k <= d && j - i * k >= 0; k++) {
        dp[i][j] += dp[i - 1][j - i * k] * Comb[n - j + i * k][i * k] % mod *
                    kaijo[i * k] % mod * inv(modpow(kaijo[i], k)) % mod *
                    inv(kaijo[k]) % mod;
        dp[i][j] %= mod;
      }
    }
  }
  cout << dp[b][n] << endl;
  return 0;
}