Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

class comb {
private:
  ll mod;
  ll mx;
  vector<ll> F;
  vector<ll> FR;

public:
  comb(ll mod = 1000000007, ll mx = 100000)
      : mod(mod), mx(mx), F(mx + 1, 1), FR(mx + 1, 1) {
    mk_F();
  }

  ll mod_pow(ll a, ll x) {
    a %= mod;
    ll ans = 1;
    while (x > 0) {
      if (x & 1) {
        ans *= a;
        ans %= mod;
      }
      a *= a;
      a %= mod;
      x >>= 1;
    }
    return ans;
  }

  pll Ex_gcd(ll a, ll b) {
    if (b == 0) {
      return {1, 0};
    }
    pll ret = Ex_gcd(b, a % b);
    ret.F -= a / b * ret.S;
    return {ret.S, ret.F};
  }

  ll prime_R(ll a) { return mod_pow(a, mod - 2); }

  ll R(ll a) {
    ll ret = Ex_gcd(a, mod).F;
    ret %= mod;
    if (ret < 0) {
      ret += mod;
    }
    return ret;
  }

  void mk_F() {
    for (ll i = 1; i <= mx; i++) {
      F[i] = F[i - 1] * i % mod;
      FR[i] = R(F[i]);
    }
  }

  ll c(ll n, ll k) {
    if (n < k) {
      return 0;
    }
    if (n == k || k == 0) {
      return 1;
    }
    return F[n] * FR[n - k] % mod * FR[k] % mod;
  }

  // mod must be prime
  ll Lucas_C(ll n, ll m) {
    ll ret = 1;
    while (n > 0 || m > 0) {
      ret *= c(n % mod, m % mod);
      ret %= mod;
      n /= mod;
      m /= mod;
    }
    return ret;
  }

  ll Stirling(ll n, ll k) {
    ll ret = 0;
    for (ll i = 1; i <= k; i++) {
      if ((k - i) % 2) {
        ret -= c(k, i) * mod_pow(i, n) % mod;
      } else {
        ret += c(k, i) * mod_pow(i, n) % mod;
      }
      ret %= mod;
    }
    ret *= R(F[k]);
    ret %= mod;
    if (ret < 0) {
      ret += mod;
    }
    return ret;
  }

  ll Bell(ll n, ll k) {
    ll ret = 0;
    for (ll i = 1; i <= k; i++) {
      ret += Stirling(n, i);
      ret %= mod;
    }
    return ret;
  }
};

comb C;

int main() {
  ll b, w;
  cin >> b >> w;
  ll p = 0, q = 0;
  ll inv = (MOD + 1) / 2;
  ll T = 1;
  ll all = 1;
  for (ll i = 0; i < b + w; i++) {
    ll ans = q * T % MOD;
    ans += (all - p - q) * T % MOD * inv % MOD;
    ans %= MOD;
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans << endl;
    all *= 2;
    all %= MOD;
    p *= 2;
    q *= 2;
    if (i + 1 >= b) {
      p += C.c(i, b - 1);
    }
    if (i + 1 >= w) {
      q += C.c(i, w - 1);
    }
    p %= MOD;
    q %= MOD;
    T *= inv;
    T %= MOD;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

class comb {
private:
  ll mod;
  ll mx;
  vector<ll> F;
  vector<ll> FR;

public:
  comb(ll mod = 1000000007, ll mx = 1000000)
      : mod(mod), mx(mx), F(mx + 1, 1), FR(mx + 1, 1) {
    mk_F();
  }

  ll mod_pow(ll a, ll x) {
    a %= mod;
    ll ans = 1;
    while (x > 0) {
      if (x & 1) {
        ans *= a;
        ans %= mod;
      }
      a *= a;
      a %= mod;
      x >>= 1;
    }
    return ans;
  }

  pll Ex_gcd(ll a, ll b) {
    if (b == 0) {
      return {1, 0};
    }
    pll ret = Ex_gcd(b, a % b);
    ret.F -= a / b * ret.S;
    return {ret.S, ret.F};
  }

  ll prime_R(ll a) { return mod_pow(a, mod - 2); }

  ll R(ll a) {
    ll ret = Ex_gcd(a, mod).F;
    ret %= mod;
    if (ret < 0) {
      ret += mod;
    }
    return ret;
  }

  void mk_F() {
    for (ll i = 1; i <= mx; i++) {
      F[i] = F[i - 1] * i % mod;
      FR[i] = R(F[i]);
    }
  }

  ll c(ll n, ll k) {
    if (n < k) {
      return 0;
    }
    if (n == k || k == 0) {
      return 1;
    }
    return F[n] * FR[n - k] % mod * FR[k] % mod;
  }

  // mod must be prime
  ll Lucas_C(ll n, ll m) {
    ll ret = 1;
    while (n > 0 || m > 0) {
      ret *= c(n % mod, m % mod);
      ret %= mod;
      n /= mod;
      m /= mod;
    }
    return ret;
  }

  ll Stirling(ll n, ll k) {
    ll ret = 0;
    for (ll i = 1; i <= k; i++) {
      if ((k - i) % 2) {
        ret -= c(k, i) * mod_pow(i, n) % mod;
      } else {
        ret += c(k, i) * mod_pow(i, n) % mod;
      }
      ret %= mod;
    }
    ret *= R(F[k]);
    ret %= mod;
    if (ret < 0) {
      ret += mod;
    }
    return ret;
  }

  ll Bell(ll n, ll k) {
    ll ret = 0;
    for (ll i = 1; i <= k; i++) {
      ret += Stirling(n, i);
      ret %= mod;
    }
    return ret;
  }
};

comb C;

int main() {
  ll b, w;
  cin >> b >> w;
  ll p = 0, q = 0;
  ll inv = (MOD + 1) / 2;
  ll T = 1;
  ll all = 1;
  for (ll i = 0; i < b + w; i++) {
    ll ans = q * T % MOD;
    ans += (all - p - q) * T % MOD * inv % MOD;
    ans %= MOD;
    if (ans < 0) {
      ans += MOD;
    }
    cout << ans << endl;
    all *= 2;
    all %= MOD;
    p *= 2;
    q *= 2;
    if (i + 1 >= b) {
      p += C.c(i, b - 1);
    }
    if (i + 1 >= w) {
      q += C.c(i, w - 1);
    }
    p %= MOD;
    q %= MOD;
    T *= inv;
    T %= MOD;
  }

  return 0;
}