Problem p03025 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define sd(x) scanf("%d", &(x))
#define pii pair<int, int>
#define F first
#define S second

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const int N = 1e5 + 10, mod = 1e9 + 7;
int fact[N], invfact[N];
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (((ll)x) * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
void pre() {
  fact[0] = invfact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(i, fact[i - 1]);
  invfact[N - 1] = inv(fact[N - 1]);
  for (int i = N - 2; i >= 1; i--)
    invfact[i] = mul(invfact[i + 1], i + 1);
  assert(invfact[1] == 1);
}

inline int C(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return mul(fact[n], mul(invfact[k], invfact[n - k]));
}

int main() {
  pre();
  int n, a, b, c;
  sd(n);
  sd(a);
  sd(b);
  sd(c);
  a = mul(a, inv(100));
  b = mul(b, inv(100));
  c = mul(c, inv(100));

  a = mul(a, inv(sub(1, c)));
  b = mul(b, inv(sub(1, c)));

  int x = 0;
  for (int i = n; i < 2 * n; i++) {
    int t = mul(C(i - 1, n - 1), mul(powr(a, n), powr(b, i - n)));
    t = add(t, mul(C(i - 1, n - 1), mul(powr(a, i - n), powr(b, n))));
    x = add(x, mul(i, t));
  }
  printf("%d\n", mul(inv(sub(1, c)), x));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define sd(x) scanf("%d", &(x))
#define pii pair<int, int>
#define F first
#define S second

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const int N = 2e5 + 10, mod = 1e9 + 7;
int fact[N], invfact[N];
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (((ll)x) * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
void pre() {
  fact[0] = invfact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(i, fact[i - 1]);
  invfact[N - 1] = inv(fact[N - 1]);
  for (int i = N - 2; i >= 1; i--)
    invfact[i] = mul(invfact[i + 1], i + 1);
  assert(invfact[1] == 1);
}

inline int C(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return mul(fact[n], mul(invfact[k], invfact[n - k]));
}

int main() {
  pre();
  int n, a, b, c;
  sd(n);
  sd(a);
  sd(b);
  sd(c);
  a = mul(a, inv(100));
  b = mul(b, inv(100));
  c = mul(c, inv(100));

  a = mul(a, inv(sub(1, c)));
  b = mul(b, inv(sub(1, c)));

  int x = 0;
  for (int i = n; i < 2 * n; i++) {
    int t = mul(C(i - 1, n - 1), mul(powr(a, n), powr(b, i - n)));
    t = add(t, mul(C(i - 1, n - 1), mul(powr(a, i - n), powr(b, n))));
    x = add(x, mul(i, t));
  }
  printf("%d\n", mul(inv(sub(1, c)), x));
}