Problem p03025 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using R = double;
using Data = pair<ll, vector<ll>>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
struct edge {
  ll from;
  ll to;
  ll cost;
};
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)
#define INF INT_MAX / 2

const int MAX = 101010;
ll fac[MAX], finv[MAX], inv[MAX];

void combinit() {
  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;
  }
}
ll 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 power(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 1) {
    return (x * power(x, n - 1)) % MOD;
  } else {
    ll half = power(x, n / 2);
    return (half * half) % MOD;
  }
}

ll N, A, B, C;
ll ans = 0;
int main() {
  cin >> N >> A >> B >> C;
  combinit();
  ll X = (A * inv[A + B]) % MOD;
  ll Y = (B * inv[A + B]) % MOD;

  for (ll i = N; i <= 2 * N - 1; i++) {
    ll temp = 1;
    temp *= i;
    temp %= MOD;
    temp *= comb(i - 1, N - 1);
    temp %= MOD;
    temp *=
        (power(X, N) * power(Y, i - N) + power(Y, N) * power(X, i - N)) % MOD;
    temp %= MOD;
    ans += temp;
    ans %= MOD;
  }

  if (C > 0) {
    ans *= (100 * inv[A + B]) % MOD;
    ans %= MOD;
  }
  cout << ans % MOD << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using R = double;
using Data = pair<ll, vector<ll>>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
struct edge {
  ll from;
  ll to;
  ll cost;
};
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)
#define INF INT_MAX / 2

const int MAX = 303030;
ll fac[MAX], finv[MAX], inv[MAX];

void combinit() {
  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;
  }
}
ll 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 power(ll x, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 1) {
    return (x * power(x, n - 1)) % MOD;
  } else {
    ll half = power(x, n / 2);
    return (half * half) % MOD;
  }
}

ll N, A, B, C;
ll ans = 0;
int main() {
  cin >> N >> A >> B >> C;
  combinit();
  ll X = (A * inv[A + B]) % MOD;
  ll Y = (B * inv[A + B]) % MOD;

  for (ll i = N; i <= 2 * N - 1; i++) {
    ll temp = 1;
    temp *= i;
    temp %= MOD;
    temp *= comb(i - 1, N - 1);
    temp %= MOD;
    temp *=
        (power(X, N) * power(Y, i - N) + power(Y, N) * power(X, i - N)) % MOD;
    temp %= MOD;
    ans += temp;
    ans %= MOD;
  }

  if (C > 0) {
    ans *= (100 * inv[A + B]) % MOD;
    ans %= MOD;
  }
  cout << ans % MOD << endl;

  return 0;
}