Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll mod = 1000000007;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (int i = (j); i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll power(ll base, ll exponent, ll module) {
  if (exponent % 2) {
    return power(base, exponent - 1, module) * base % module;
  } else if (exponent) {
    ll root_ans = power(base, exponent / 2, module);
    return root_ans * root_ans % module;
  } else {
    return 1;
  }
}
struct combination {
  vector<ll> fact, inv;
  combination(int sz) : fact(sz + 1), inv(sz + 1) {
    fact[0] = 1;
    for (int i = 1; i <= sz; i++) {
      fact[i] = fact[i - 1] * i % mod;
    }
    inv[sz] = power(fact[sz], mod - 2, mod);
    for (int i = sz - 1; i >= 0; i--) {
      inv[i] = inv[i + 1] * (i + 1) % mod;
    }
  }
  ll C(int p, int q) const {
    if (q < 0 || p < q)
      return 0;
    return (fact[p] * inv[q] % mod * inv[p - q] % mod);
  }
};

ll inverse(ll x) { return power(x, mod - 2, mod); }

combination table(1e6);

ll b;
ll w;

signed main() {
  ios::sync_with_stdio(false);
  cin >> b >> w;
  vector<ll> p(b + w);
  vector<ll> q(b + w);
  ll ans = inverse(2);
  p[w] = inverse(power(2, w, mod));
  REP_FROM(i, w, b + w) {
    p[i + 1] =
        p[i] + table.C(i, w - 1) * inverse(power(2, i + 1, mod) % mod) % mod;
    p[i + 1] %= mod;
  }
  q[b] = inverse(power(2, b, mod));
  REP_FROM(i, b, b + w) {
    q[i + 1] =
        q[i] + table.C(i, b - 1) * inverse(power(2, i + 1, mod) % mod) % mod;
    q[i + 1] %= mod;
  }
  REP(i, b + w) {
    ll ans = inverse(2);
    ans += (inverse(2) * p[i]) % mod;
    ans %= mod;
    ans -= (inverse(2) * q[i]) % mod;
    if (ans < 0)
      ans += mod;
    ans %= mod;
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll mod = 1000000007;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (int i = (j); i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll power(ll base, ll exponent, ll module) {
  if (exponent % 2) {
    return power(base, exponent - 1, module) * base % module;
  } else if (exponent) {
    ll root_ans = power(base, exponent / 2, module);
    return root_ans * root_ans % module;
  } else {
    return 1;
  }
}
struct combination {
  vector<ll> fact, inv;
  combination(int sz) : fact(sz + 1), inv(sz + 1) {
    fact[0] = 1;
    for (int i = 1; i <= sz; i++) {
      fact[i] = fact[i - 1] * i % mod;
    }
    inv[sz] = power(fact[sz], mod - 2, mod);
    for (int i = sz - 1; i >= 0; i--) {
      inv[i] = inv[i + 1] * (i + 1) % mod;
    }
  }
  ll C(int p, int q) const {
    if (q < 0 || p < q)
      return 0;
    return (fact[p] * inv[q] % mod * inv[p - q] % mod);
  }
};

ll inverse(ll x) { return power(x, mod - 2, mod); }

combination table(1e6);

ll b;
ll w;

signed main() {
  ios::sync_with_stdio(false);
  cin >> b >> w;
  vector<ll> p(b + w + 1);
  vector<ll> q(b + w + 1);
  ll ans = inverse(2);
  p[w] = inverse(power(2, w, mod));
  REP_FROM(i, w, b + w) {
    p[i + 1] =
        p[i] + table.C(i, w - 1) * inverse(power(2, i + 1, mod) % mod) % mod;
    p[i + 1] %= mod;
  }
  q[b] = inverse(power(2, b, mod));
  REP_FROM(i, b, b + w) {
    q[i + 1] =
        q[i] + table.C(i, b - 1) * inverse(power(2, i + 1, mod) % mod) % mod;
    q[i + 1] %= mod;
  }
  REP(i, b + w) {
    ll ans = inverse(2);
    ans += (inverse(2) * p[i]) % mod;
    ans %= mod;
    ans -= (inverse(2) * q[i]) % mod;
    if (ans < 0)
      ans += mod;
    ans %= mod;
    cout << ans << endl;
  }
}