Problem p03083 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

vector<long long int> inverse;
vector<long long int> f, f_inverse;

void factorial_Initialize(int max) {
  vector<long long int> a(max + 1, 1);
  inverse = a;
  f = a;
  f_inverse = a;

  for (int i = 2; i <= max; i++) {
    inverse[i] = MOD - inverse[MOD % i] * (MOD / i) % MOD;
    f[i] = f[i - 1] * i % MOD;
    f_inverse[i] = f_inverse[i - 1] * inverse[i] % MOD;
  }
}

long long int nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  if (n >= f.size()) {
    factorial_Initialize(n);
  }
  return f[n] * f_inverse[r] % MOD * f_inverse[n - r] % MOD;
}

long long int modpow(long long int b,
                     long long int p) { // modinv(x) = modpow(x, MOD-2)
  long long int num = 1, power = b;
  for (int i = 0; i <= 30; i++) {
    if ((p >> i) & 1) {
      num *= power;
      num %= MOD;
    }

    power *= power;
    power %= MOD;
  }
  return num;
}

int main() {
  long long int b, w;
  cin >> b >> w;
  cout << modpow(2, MOD - 2) << endl;

  vector<long long int> p(b + w), q(b + w);
  for (int i = 1; i < b + w; i++) {
    p[i] = p[i - 1] + nCr(i - 1, b - 1) * modpow(modpow(2, MOD - 2), i - 1) %
                          MOD * modpow(2, MOD - 2);
    p[i] %= MOD;
    q[i] = q[i - 1] + nCr(i - 1, w - 1) * modpow(modpow(2, MOD - 2), i - 1) %
                          MOD * modpow(2, MOD - 2);
    q[i] %= MOD;

    long long int ans =
        ((1 - p[i] - q[i]) % MOD + MOD) % MOD * modpow(2, MOD - 2) + q[i] * 1;
    ans %= MOD;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

vector<long long int> inverse;
vector<long long int> f, f_inverse;

void factorial_Initialize(int max) {
  vector<long long int> a(max + 1, 1);
  inverse = a;
  f = a;
  f_inverse = a;

  for (int i = 2; i <= max; i++) {
    inverse[i] = MOD - inverse[MOD % i] * (MOD / i) % MOD;
    f[i] = f[i - 1] * i % MOD;
    f_inverse[i] = f_inverse[i - 1] * inverse[i] % MOD;
  }
}

long long int nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  if (n >= f.size()) {
    factorial_Initialize(n);
  }
  return f[n] * f_inverse[r] % MOD * f_inverse[n - r] % MOD;
}

long long int modpow(long long int b,
                     long long int p) { // modinv(x) = modpow(x, MOD-2)
  long long int num = 1, power = b;
  for (int i = 0; i <= 30; i++) {
    if ((p >> i) & 1) {
      num *= power;
      num %= MOD;
    }

    power *= power;
    power %= MOD;
  }
  return num;
}

int main() {
  long long int b, w;
  cin >> b >> w;
  cout << modpow(2, MOD - 2) << endl;
  factorial_Initialize(1e6);

  vector<long long int> p(b + w), q(b + w);
  for (int i = 1; i < b + w; i++) {
    p[i] = p[i - 1] + nCr(i - 1, b - 1) * modpow(modpow(2, MOD - 2), i - 1) %
                          MOD * modpow(2, MOD - 2);
    p[i] %= MOD;
    q[i] = q[i - 1] + nCr(i - 1, w - 1) * modpow(modpow(2, MOD - 2), i - 1) %
                          MOD * modpow(2, MOD - 2);
    q[i] %= MOD;

    long long int ans =
        ((1 - p[i] - q[i]) % MOD + MOD) % MOD * modpow(2, MOD - 2) + q[i] * 1;
    ans %= MOD;
    cout << ans << endl;
  }
  return 0;
}