Problem p03025 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

long long n, a, b, c;
long long MOD = 1e9 + 7;
long long inq(long long a, long long b) {
  if (b == 0)
    return 1;
  long long l = inq(a, b / 2);
  if (b % 2)
    return l * l % MOD * a % MOD;
  return l * l % MOD;
}
long long fact[500000], defact[500000];
long long cnk(long long n, long long k) {
  return fact[n] * defact[k] % MOD * defact[n - k] % MOD;
}
long long dwn = 1;
long long g[500000];
long long success[500000];
long long cnk2(long long n, long long k) {
  long long fg = inq(a, k) * inq(b, n - k) % MOD *
                 inq(inq((a + b) * inq(2, MOD - 2), n), MOD - 2) % MOD;
  return cnk(n, k) * dwn % MOD * fg % MOD * inq(g[n + 1], MOD - 2) % MOD;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> a >> b >> c;
  fact[0] = 1, defact[0] = 1;
  for (int i = 1; i < 2 * n; i++)
    fact[i] = fact[i - 1] * i % MOD, defact[i] = inq(fact[i], MOD - 2);
  for (int i = n; i < 2 * n; i++) {
    g[i] = inq(2, i - 1);
  }
  for (int i = n; i < 2 * n; i++)
    dwn = dwn * g[i] % MOD;
  dwn = dwn * inq((a + b), 2 * n - 1) % MOD;
  long long up = 0, sup = 0;
  for (int i = n; i < 2 * n; i++) {
    long long dup = 0;
    for (int x = n - 1; x <= n - 1; x++) {
      if (x == n - 1 && i - 1 - x <= n - 1) {
        dup = (dup + cnk2(i - 1, x) * a % MOD * inq(a + b, MOD - 2)) % MOD;
      }
    }
    for (int x = i - n; x <= i - n; x++) {
      if (x <= n - 1 && i - 1 - x == n - 1) {
        dup = (dup + cnk2(i - 1, x) * b % MOD * inq(a + b, MOD - 2)) % MOD;
      }
    }
    up = (up + i * dup) % MOD;
    sup = (sup + dup) % MOD;
  }
  if (sup != dwn) {
    cout << 100 / 0;
  }
  up = up * (a + b + c) % MOD;
  dwn = dwn * (a + b) % MOD;
  cout << up * inq(dwn, MOD - 2) % MOD;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

long long n, a, b, c;
long long MOD = 1e9 + 7;
long long inq(long long a, long long b) {
  if (b == 0)
    return 1;
  long long l = inq(a, b / 2);
  if (b % 2)
    return l * l % MOD * a % MOD;
  return l * l % MOD;
}
long long fact[500000], defact[500000];
long long cnk(long long n, long long k) {
  return fact[n] * defact[k] % MOD * defact[n - k] % MOD;
}
long long dwn = 1;
long long g[500000];
long long success[500000];
long long cnk2(long long n, long long k) {
  long long fg = inq(a, k) * inq(b, n - k) % MOD *
                 inq(inq((a + b) * inq(2, MOD - 2) % MOD, n), MOD - 2) % MOD;
  return cnk(n, k) * dwn % MOD * fg % MOD * inq(g[n + 1], MOD - 2) % MOD;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> a >> b >> c;
  fact[0] = 1, defact[0] = 1;
  for (int i = 1; i < 2 * n; i++)
    fact[i] = fact[i - 1] * i % MOD, defact[i] = inq(fact[i], MOD - 2);
  for (int i = n; i < 2 * n; i++) {
    g[i] = inq(2, i - 1);
  }
  for (int i = n; i < 2 * n; i++)
    dwn = dwn * g[i] % MOD;
  dwn = dwn * inq((a + b), 2 * n - 1) % MOD;
  long long up = 0, sup = 0;
  for (int i = n; i < 2 * n; i++) {
    long long dup = 0;
    for (int x = n - 1; x <= n - 1; x++) {
      if (x == n - 1 && i - 1 - x <= n - 1) {
        dup = (dup + cnk2(i - 1, x) * a % MOD * inq(a + b, MOD - 2)) % MOD;
      }
    }
    for (int x = i - n; x <= i - n; x++) {
      if (x <= n - 1 && i - 1 - x == n - 1) {
        dup = (dup + cnk2(i - 1, x) * b % MOD * inq(a + b, MOD - 2)) % MOD;
      }
    }
    up = (up + i * dup) % MOD;
    sup = (sup + dup) % MOD;
  }
  if (sup != dwn) {
    cout << 100 / 0;
  }
  up = up * (a + b + c) % MOD;
  dwn = dwn * (a + b) % MOD;
  cout << up * inq(dwn, MOD - 2) % MOD;
}