Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 100002;

long long fac[MAX], finv[MAX], inv[MAX];

void pre_comb(void) {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

ll mpow(ll x, ll r) {
  if (r == 1)
    return x;
  if (r % 2 == 0) {
    ll aa = mpow(x, r / 2);
    return aa * aa % mod;
  } else {
    ll aa = mpow(x, r / 2);
    return (aa * aa % mod) * x % mod;
  }
}

int main() {
  pre_comb();
  int B, W;
  cin >> B >> W;
  ll sum1[B + W], sum2[B + W], sum3[B + W], two[B + W + 1];
  rep(i, B + W + 1) {
    if (i == 0)
      two[i] = 1;
    else
      two[i] = two[i - 1] * 500000004 % mod;
  }
  rep(i, B + W) {
    if (i == 0) {
      if (W == 1)
        sum2[i] = 500000004;
      else
        sum2[i] = 0;
      sum1[i] = 0;
      sum3[i] = 0;
    } else {
      if (i + 1 >= B && i < B + W - 1) {
        sum1[i] = sum1[i - 1] + comb(i - 1, B - 2) * two[i + 1] % mod;
      } else {
        sum1[i] = sum1[i - 1];
      }
      if (i + 1 >= W && i < B + W - 1) {
        sum2[i] = sum2[i - 1] + comb(i, W - 1) * two[i + 1] % mod;
      } else {
        sum2[i] = sum2[i - 1];
      }
      if (i >= W && i < B + W - 1) {
        sum3[i] = sum3[i - 1] + comb(i - 1, W - 1) * two[i + 1] % mod;
      } else {
        sum3[i] = sum3[i - 1];
      }
    }
  }
  rep(i, B + W) {
    ll ans = 0;
    if (i > 0)
      ans = (ans + sum2[i - 1]) % mod;
    ans = (ans + sum3[B + W - 1] - sum3[i]) % mod;
    if (i < B + W - 1)
      ans = (ans + sum1[B + W - 1] - sum1[i]) % mod;
    if (i + 1 >= B && i < B + W - 1)
      ans = (ans + comb(i, B - 1) * two[i + 1] % mod) % mod;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 200002;

long long fac[MAX], finv[MAX], inv[MAX];

void pre_comb(void) {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

ll mpow(ll x, ll r) {
  if (r == 1)
    return x;
  if (r % 2 == 0) {
    ll aa = mpow(x, r / 2);
    return aa * aa % mod;
  } else {
    ll aa = mpow(x, r / 2);
    return (aa * aa % mod) * x % mod;
  }
}

int main() {
  pre_comb();
  int B, W;
  cin >> B >> W;
  ll sum1[B + W], sum2[B + W], sum3[B + W], two[B + W + 1];
  rep(i, B + W + 1) {
    if (i == 0)
      two[i] = 1;
    else
      two[i] = two[i - 1] * 500000004 % mod;
  }
  rep(i, B + W) {
    if (i == 0) {
      if (W == 1)
        sum2[i] = 500000004;
      else
        sum2[i] = 0;
      sum1[i] = 0;
      sum3[i] = 0;
    } else {
      if (i + 1 >= B && i < B + W - 1) {
        sum1[i] = sum1[i - 1] + comb(i - 1, B - 2) * two[i + 1] % mod;
      } else {
        sum1[i] = sum1[i - 1];
      }
      if (i + 1 >= W && i < B + W - 1) {
        sum2[i] = sum2[i - 1] + comb(i, W - 1) * two[i + 1] % mod;
      } else {
        sum2[i] = sum2[i - 1];
      }
      if (i >= W && i < B + W - 1) {
        sum3[i] = sum3[i - 1] + comb(i - 1, W - 1) * two[i + 1] % mod;
      } else {
        sum3[i] = sum3[i - 1];
      }
    }
  }
  rep(i, B + W) {
    ll ans = 0;
    if (i > 0)
      ans = (ans + sum2[i - 1]) % mod;
    ans = (ans + sum3[B + W - 1] - sum3[i]) % mod;
    if (i < B + W - 1)
      ans = (ans + sum1[B + W - 1] - sum1[i]) % mod;
    if (i + 1 >= B && i < B + W - 1)
      ans = (ans + comb(i, B - 1) * two[i + 1] % mod) % mod;
    cout << ans << endl;
  }
  return 0;
}