Problem p03025 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

#define MAX_N (100000L)
#define MAX_FACT (MAX_N)
#define NUM_MOD (1000000007L)

long fs[MAX_FACT + 1], finvs[MAX_FACT + 1], invs[MAX_FACT + 1];

void combination_init() {
  fs[0] = fs[1] = 1;
  finvs[0] = finvs[1] = 1;
  invs[1] = 1;
  for (long i = 2; i <= MAX_FACT; i++) {
    fs[i] = fs[i - 1] * i % NUM_MOD;
    invs[i] = NUM_MOD - invs[NUM_MOD % i] * (NUM_MOD / i) % NUM_MOD;
    finvs[i] = finvs[i - 1] * invs[i] % NUM_MOD;
  }
}

long combination(long n, long k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fs[n] * (finvs[k] * finvs[n - k] % NUM_MOD) % NUM_MOD;
}

long mod(long n, long k) { return n % k >= 0 ? n % k : n % k + k; }

long pow_mod(long x, long k, long mod) {
  long ret = 1, p = x;
  while (k > 0) {
    if (k & 1) {
      ret = (ret * p) % mod;
    }
    p = (p * p) % mod;
    k /= 2;
  }
  return ret;
}

int main(int argc, char *argv[]) {
  // read inputs
  long N, A, B, C;
  scanf("%ld %ld %ld %ld", &N, &A, &B, &C);

  // solve
  long ans = 0;
  combination_init();
  // part1
  long x1 = 0;
  for (long k = 0; k <= N - 1; k++) {
    x1 = mod(mod(mod(pow_mod(mod(A * invs[A + B], NUM_MOD), N, NUM_MOD) *
                         pow_mod(mod(B * invs[A + B], NUM_MOD), k, NUM_MOD),
                     NUM_MOD) *
                     combination(N + k, k),
                 NUM_MOD) +
                 x1,
             NUM_MOD);
  }
  x1 = mod(N * x1, NUM_MOD);
  // part2
  long x2 = 0;
  for (long k = 0; k <= N - 1; k++) {
    x2 = mod(mod(mod(pow_mod(mod(A * invs[A + B], NUM_MOD), k, NUM_MOD) *
                         pow_mod(mod(B * invs[A + B], NUM_MOD), N, NUM_MOD),
                     NUM_MOD) *
                     combination(N + k, k),
                 NUM_MOD) +
                 x2,
             NUM_MOD);
  }
  x2 = mod(N * x2, NUM_MOD);
  printf("%ld\n",
         mod(mod(mod(x1 + x2, NUM_MOD) * 100, NUM_MOD) * invs[A + B], NUM_MOD));

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#define MAX_N (300000L)
#define MAX_FACT (MAX_N)
#define NUM_MOD (1000000007L)

long fs[MAX_FACT + 1], finvs[MAX_FACT + 1], invs[MAX_FACT + 1];

void combination_init() {
  fs[0] = fs[1] = 1;
  finvs[0] = finvs[1] = 1;
  invs[1] = 1;
  for (long i = 2; i <= MAX_FACT; i++) {
    fs[i] = fs[i - 1] * i % NUM_MOD;
    invs[i] = NUM_MOD - invs[NUM_MOD % i] * (NUM_MOD / i) % NUM_MOD;
    finvs[i] = finvs[i - 1] * invs[i] % NUM_MOD;
  }
}

long combination(long n, long k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fs[n] * (finvs[k] * finvs[n - k] % NUM_MOD) % NUM_MOD;
}

long mod(long n, long k) { return n % k >= 0 ? n % k : n % k + k; }

long pow_mod(long x, long k, long mod) {
  long ret = 1, p = x;
  while (k > 0) {
    if (k & 1) {
      ret = (ret * p) % mod;
    }
    p = (p * p) % mod;
    k /= 2;
  }
  return ret;
}

int main(int argc, char *argv[]) {
  // read inputs
  long N, A, B, C;
  scanf("%ld %ld %ld %ld", &N, &A, &B, &C);

  // solve
  long ans = 0;
  combination_init();
  // part1
  long x1 = 0;
  for (long k = 0; k <= N - 1; k++) {
    x1 = mod(mod(mod(pow_mod(mod(A * invs[A + B], NUM_MOD), N, NUM_MOD) *
                         pow_mod(mod(B * invs[A + B], NUM_MOD), k, NUM_MOD),
                     NUM_MOD) *
                     combination(N + k, k),
                 NUM_MOD) +
                 x1,
             NUM_MOD);
  }
  x1 = mod(N * x1, NUM_MOD);
  // part2
  long x2 = 0;
  for (long k = 0; k <= N - 1; k++) {
    x2 = mod(mod(mod(pow_mod(mod(A * invs[A + B], NUM_MOD), k, NUM_MOD) *
                         pow_mod(mod(B * invs[A + B], NUM_MOD), N, NUM_MOD),
                     NUM_MOD) *
                     combination(N + k, k),
                 NUM_MOD) +
                 x2,
             NUM_MOD);
  }
  x2 = mod(N * x2, NUM_MOD);
  printf("%ld\n",
         mod(mod(mod(x1 + x2, NUM_MOD) * 100, NUM_MOD) * invs[A + B], NUM_MOD));

  return 0;
}