Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 7;
const int MOD = 1000 * 1000 * 1000 + 7;
int mod(int n) { return (n % MOD + MOD) % MOD; }
int fp(int n, int p) {
  int ans = 1, c = n;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * c);
    c = mod(c * c);
  }
  return ans;
}
int mdiv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
struct F {
  int a, b;
  F(int a_, int b_) {
    a = a_;
    b = b_;
  }
  F() {}
  F operator+(F p) { return {mod(a * p.b + p.a * b), mod(b * p.b)}; }
  F operator-(F p) { return {mod(a * p.b - p.a * b), mod(b * p.b)}; }
  F operator*(F p) { return {mod(a * p.a), mod(b * p.b)}; }
  F operator/(F p) { return {mod(a * p.b), mod(b * p.a)}; }
  F operator*(int p) { return {mod(a * p), b}; }
  F operator/(int p) { return {a, mod(b * p)}; }
};
int f[N];
void prec() {
  f[0] = 1;
  for (int i = 1; i < N; ++i) {
    f[i] = mod(f[i - 1] * i);
  }
}
F C(int n, int k) {
  if (n < k)
    return {0, 1};
  else
    return {f[n], mod(f[k] * f[n - k])};
}
int B, W;
F all_black[N], all_white[N];
F one_black(int h) {
  int del = B - 1;
  return C(h, del) / fp(2, h);
}
F one_white(int h) {
  int del = W - 1;
  return C(h, del) / fp(2, h);
}
void print(F p) { cout << "F " << p.a << ' ' << p.b << '\n'; }
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  prec();
  cin >> B >> W;
  all_white[0] = all_black[0] = {0, 1};
  for (int i = 1; i < W + B; ++i) {
    all_white[i] = all_white[i - 1] + one_black(i - 1) / 2;
    all_black[i] = all_black[i - 1] + one_white(i - 1) / 2;
  }
  for (int i = 0; i < W + B; ++i) {
    if (!all_white[i].a && !all_black[i].a) {
      cout << mdiv(1, 2) << '\n';
    } else {
      F ans = {0, 1};
      ans = ans + all_black[i];
      ans = ans + (F(1, 1) - all_white[i] - all_black[i]) / 2;

#ifdef HOME
      print(ans);
#endif

      cout << mdiv(ans.a, ans.b) << '\n';
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 7;
const int MOD = 1000 * 1000 * 1000 + 7;
int mod(int n) { return (n % MOD + MOD) % MOD; }
int fp(int n, int p) {
  int ans = 1, c = n;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * c);
    c = mod(c * c);
  }
  return ans;
}
int mdiv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
struct F {
  int a, b;
  F(int a_, int b_) {
    a = a_;
    b = b_;
  }
  F() {}
  F operator+(F p) { return {mod(a * p.b + p.a * b), mod(b * p.b)}; }
  F operator-(F p) { return {mod(a * p.b - p.a * b), mod(b * p.b)}; }
  F operator*(F p) { return {mod(a * p.a), mod(b * p.b)}; }
  F operator/(F p) { return {mod(a * p.b), mod(b * p.a)}; }
  F operator*(int p) { return {mod(a * p), b}; }
  F operator/(int p) { return {a, mod(b * p)}; }
};
int f[N];
void prec() {
  f[0] = 1;
  for (int i = 1; i < N; ++i) {
    f[i] = mod(f[i - 1] * i);
  }
}
F C(int n, int k) {
  if (n < k)
    return {0, 1};
  else
    return {f[n], mod(f[k] * f[n - k])};
}
int B, W;
F all_black[N], all_white[N];
F one_black(int h) {
  int del = B - 1;
  return C(h, del) / fp(2, h);
}
F one_white(int h) {
  int del = W - 1;
  return C(h, del) / fp(2, h);
}
void print(F p) { cout << "F " << p.a << ' ' << p.b << '\n'; }
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  prec();
  cin >> B >> W;
  all_white[0] = all_black[0] = {0, 1};
  for (int i = 1; i < W + B; ++i) {
    all_white[i] = all_white[i - 1] + one_black(i - 1) / 2;
    all_black[i] = all_black[i - 1] + one_white(i - 1) / 2;
  }
  for (int i = 0; i < W + B; ++i) {
    if (!all_white[i].a && !all_black[i].a) {
      cout << mdiv(1, 2) << '\n';
    } else {
      F ans = {0, 1};
      ans = ans + all_black[i];
      ans = ans + (F(1, 1) - all_white[i] - all_black[i]) / 2;

#ifdef HOME
      print(ans);
#endif

      cout << mdiv(ans.a, ans.b) << '\n';
    }
  }
}