Problem:


Score : 500 points

Problem Statement

Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.

When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %.
Find the expected number of games that will be played, and print it as follows.

We can represent the expected value as P/Q with coprime integers P and Q.
Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}.
(Such an integer R always uniquely exists under the constraints of this problem.)

Constraints

1 \leq N \leq 100000

0 \leq A,B,C \leq 100

1 \leq A+B

A+B+C=100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C

Output

Print the expected number of games that will be played, in the manner specified in the statement.

Sample Input 1

1 25 25 50

Sample Output 1

2

Since N=1, they will repeat the game until one of them wins.
The expected number of games played is 2.

Sample Input 2

4 50 50 0

Sample Output 2

312500008

C may be 0.

Sample Input 3

1 100 0 0

Sample Output 3

1

B may also be 0.

Sample Input 4

100000 31 41 28

Sample Output 4

104136146



Sample input:
1 25 25 50


Sample output:
2

Submission status: Runtime Error

Solution code:
import itertools
import os
import sys

sys.setrecursionlimit(2147483647)
INF = float('inf')

if os.getenv('LOCAL'):
    sys.stdin = open('_in.txt')


def mod_inv(a):
    """
    a の逆元
    :param int a:
    :return:
    """
    return pow(a, MOD - 2, MOD)


# https://atcoder.jp/contests/abc066/submissions/5721975
def mod_invs(max, mod):
    """
    逆元のリスト 0 から max まで
    :param max:
    :param mod:
    :return:
    """
    invs = [1] * (max + 1)
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def factorials(max, mod=None):
    """
    階乗 0!, 1!, 2!, ..., max!
    :param int max:
    :param int mod:
    :return:
    """
    ret = [1]
    n = 1
    if mod:
        for i in range(1, max + 1):
            n *= i
            n %= mod
            ret.append(n)
    else:
        for i in range(1, max + 1):
            n *= i
            ret.append(n)
    return ret


MOD = 10 ** 9 + 7
N, A, B, C = list(map(int, sys.stdin.readline().split()))

# R * Q % MOD == P ってことは
# P * inv(Q) % MOD == R なのでこれを求めればいい

# --- ふつうに期待値求めてみる ---
# # 引き分けなしの値にする
# A /= 100 - C
# B /= 100 - C
# C /= 100
#
# # 引き分けなしで A が勝つときの回数の期待値
# a = 0
# for i in range(N):
#     a += A ** N * B ** i * math.factorial(N + i - 1) / math.factorial(N - 1) / math.factorial(i) * (N + i)
# # 引き分けなしで B が勝つときの回数の期待値
# b = 0
# for i in range(N):
#     b += B ** N * A ** i * math.factorial(N + i - 1) / math.factorial(N - 1) / math.factorial(i) * (N + i)
#
# # C 以外が起こる期待値なので 1/(1-C) をかける
# ans = (a + b) / (1 - C)
# print(ans)


# --- 愚直解 ---
# ans = 0
# # 引き分けなしで A が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(A * mod_inv(100 - C), N, MOD) \
#            * pow(B * mod_inv(100 - C), i, MOD) \
#            * math.factorial(N + i - 1) \
#            * mod_inv(math.factorial(N - 1) * math.factorial(i)) \
#            * (N + i)
#     ans %= MOD
# # 引き分けなしで B が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(B * mod_inv(100 - C), N, MOD) \
#            * pow(A * mod_inv(100 - C), i, MOD) \
#            * math.factorial(N + i - 1) \
#            * mod_inv(math.factorial(N - 1) * math.factorial(i)) \
#            * (N + i)
#     ans %= MOD
# # 引き分けを考慮
# ans *= 100 * mod_inv(100 - C)
# print(ans % MOD)


# invs = mod_invs(max=max(N, 100), mod=MOD)
# fs = factorials(max=2 * N, mod=MOD)
# ans = 0
# # 引き分けなしで A が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(A * invs[100 - C], N, MOD) \
#            * pow(B * invs[100 - C], i, MOD) \
#            * fs[N + i - 1] \
#            * mod_inv(fs[N - 1] * fs[i]) \
#            * (N + i)
#     ans %= MOD
# # 引き分けなしで B が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(B * invs[100 - C], N, MOD) \
#            * pow(A * invs[100 - C], i, MOD) \
#            * fs[N + i - 1] \
#            * mod_inv(fs[N - 1] * fs[i]) \
#            * (N + i) % MOD
#     ans %= MOD
# # 引き分けを考慮
# ans *= 100 * invs[100 - C]
# print(ans % MOD)


invs = mod_invs(max=max(N, 100), mod=MOD)
fs = factorials(max=2 * N, mod=MOD)
A *= invs[100 - C]
B *= invs[100 - C]
A_powers = list(itertools.accumulate([1] * (N + 1), lambda p, _: p * A % MOD))
B_powers = list(itertools.accumulate([1] * (N + 1), lambda p, _: p * B % MOD))
ans = 0
for i in range(N):
    ans += ((A_powers[N] * B_powers[i] + B_powers[N] * A_powers[i])
            * fs[N + i - 1] * mod_inv(fs[N - 1] * fs[i])
            * (N + i))
    ans %= MOD
# 引き分けを考慮
ans *= 100 * invs[100 - C]
print(ans % MOD)

Submission status: Runtime Error

Solution code:
import os
import sys
import numpy as np

sys.setrecursionlimit(2147483647)
INF = float('inf')

if os.getenv('LOCAL'):
    sys.stdin = open('_in.txt')


def mod_inv(a):
    """
    a の逆元
    :param int a:
    :return:
    """
    return pow(a, MOD - 2, MOD)


# https://atcoder.jp/contests/abc066/submissions/5721975
def mod_invs(max, mod):
    """
    逆元のリスト 0 から max まで
    :param max:
    :param mod:
    :return:
    """
    invs = [1] * (max + 1)
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def factorials(max, mod=None):
    """
    階乗 0!, 1!, 2!, ..., max!
    :param int max:
    :param int mod:
    :return:
    """
    ret = [1]
    n = 1
    if mod:
        for i in range(1, max + 1):
            n *= i
            n %= mod
            ret.append(n)
    else:
        for i in range(1, max + 1):
            n *= i
            ret.append(n)
    return ret


MOD = 10 ** 9 + 7
N, A, B, C = list(map(int, sys.stdin.readline().split()))
# R * Q % MOD == P ってことは
# P * inv(Q) % MOD == R なのでこれを求めればいい

# --- ふつうに期待値求めてみる ---
# # 引き分けなしの値にする
# A /= 100 - C
# B /= 100 - C
# C /= 100
#
# # 引き分けなしで A が勝つときの回数の期待値
# a = 0
# for i in range(N):
#     a += A ** N * B ** i * math.factorial(N + i - 1) / math.factorial(N - 1) / math.factorial(i) * (N + i)
# # 引き分けなしで B が勝つときの回数の期待値
# b = 0
# for i in range(N):
#     b += B ** N * A ** i * math.factorial(N + i - 1) / math.factorial(N - 1) / math.factorial(i) * (N + i)
#
# # C 以外が起こる期待値なので 1/(1-C) をかける
# ans = (a + b) / (1 - C)
# print(ans)


# --- 愚直解 ---
# ans = 0
# # 引き分けなしで A が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(A * mod_inv(100 - C), N, MOD) \
#            * pow(B * mod_inv(100 - C), i, MOD) \
#            * math.factorial(N + i - 1) \
#            * mod_inv(math.factorial(N - 1) * math.factorial(i)) \
#            * (N + i)
#     ans %= MOD
# # 引き分けなしで B が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(B * mod_inv(100 - C), N, MOD) \
#            * pow(A * mod_inv(100 - C), i, MOD) \
#            * math.factorial(N + i - 1) \
#            * mod_inv(math.factorial(N - 1) * math.factorial(i)) \
#            * (N + i)
#     ans %= MOD
# # 引き分けを考慮
# ans *= 100 * mod_inv(100 - C)
# print(ans % MOD)


# invs = mod_invs(max=max(N, 100), mod=MOD)
# fs = factorials(max=2 * N, mod=MOD)
# ans = 0
# # 引き分けなしで A が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(A * invs[100 - C], N, MOD) \
#            * pow(B * invs[100 - C], i, MOD) \
#            * fs[N + i - 1] \
#            * mod_inv(fs[N - 1] * fs[i]) \
#            * (N + i)
#     ans %= MOD
# # 引き分けなしで B が勝つときの回数の期待値
# for i in range(N):
#     ans += pow(B * invs[100 - C], N, MOD) \
#            * pow(A * invs[100 - C], i, MOD) \
#            * fs[N + i - 1] \
#            * mod_inv(fs[N - 1] * fs[i]) \
#            * (N + i) % MOD
#     ans %= MOD
# # 引き分けを考慮
# ans *= 100 * invs[100 - C]
# print(ans % MOD)


invs = mod_invs(max=max(N, 100), mod=MOD)
fs = factorials(max=2 * N, mod=MOD)
ans = 0
for i in range(N):
    ans += ((pow(A * invs[100 - C], N, MOD) * pow(B * invs[100 - C], i, MOD)
             + pow(B * invs[100 - C], N, MOD) * pow(A * invs[100 - C], i, MOD))
            * fs[N + i - 1] * mod_inv(fs[N - 1] * fs[i])
            * (N + i))
    ans %= MOD
# 引き分けを考慮
ans *= 100 * invs[100 - C]
print(ans % MOD)

Submission status: Runtime Error

Solution code:
from fractions import gcd
setrecursionlimit(10**9)

def inpl(): return list(map(int, input().split()))
N, A, B, C = inpl()

MOD = 10**9 + 7
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

size = 2*100000 + 1
g1, g2, inverse = [0]*size, [0]*size, [0]*size

g1[:2] = [1, 1] # 元テーブル
g2[:2] = [1, 1] #逆元テーブル
inverse[:2] = [0, 1] #逆元テーブル計算用テーブル
 
for i in range(2, size):
    g1[i] =  ( g1[i-1] * i ) % MOD 
    inverse[i] = (-inverse[MOD % i] * (MOD//i) ) % MOD 
    g2[i] =  (g2[i-1] * inverse[i]) % MOD

P = 0
Q = pow(100-C, 2*N, MOD)*(100-C)%MOD

for x in range(N):
    P = (P + cmb(N+x-1, x, MOD) * (N+x) * pow(100-C, N-x, MOD) * (pow(inverse[A], N-x, MOD) + pow(inverse[B], N-x, MOD)))%MOD
P = P*100*pow(A, N, MOD)*pow(B, N, MOD)%MOD

gcd_ = gcd(P, Q)
P = P//gcd_
Q = Q//gcd_

if A*B == 0:
    print(N*100*pow(100-C, MOD-2, MOD)%MOD)
else:
    print(P*pow(Q, MOD-2, MOD)%MOD)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(110000)


def inpl():
    return list(map(int, input().split()))


class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return


N = int(input())
uf = UnionFind(N)
AB = [[0, 0] for i in range(N - 1)]

for i in range(N - 1):
    a, b = inpl()
    a -= 1
    b -= 1
    AB[i][0] = a
    AB[i][1] = b
    uf.adj[a].append(b)
    uf.adj[b].append(a)

C = inpl()

for a, b in AB:
    uf.union(a, b)

root_num = uf.find_root()
uf.hop[root_num] = 0
uf.dfs(root_num)

dic2 = sorted(uf.hop.items(), key=lambda x: x[1], reverse=True)

ans = [0 for i in range(N)]
for i, k in enumerate(dic2):
    ans[k[0]] = i

C.sort()

print(sum(C[:-1]))
for i in range(N):
    if i != 0:
        print(" ", end="")
    print(C[ans[i]], end="")
print("")

Submission status: Runtime Error

Solution code:
# WARN: Require mod number m is prime number
class ModComb:
    # For nCr mod m
    def __init__(self, n_max, m):
        self._n_max = n_max
        self._m = m
        self._mod_facts = [1] * (n_max+1)       # list of n! mod m
        self._mod_inv_facts = [1] * (n_max+1)   # list of inverse r! mod m

        if not m >= 3:
            raise ValueError('Require mod number m >= 3')

        # Calc n_max! mod m
        for i in range(2, n_max+1):
            self._mod_facts[i] = self._mod_facts[i-1] * i % m

        # Calc inverse r! mod m
        self._mod_inv_facts[n_max] = pow(self._mod_facts[n_max], m-2, m)
        for i in range(n_max-1, 1, -1):
            self._mod_inv_facts[i] = self._mod_inv_facts[i+1] * (i+1) % m

    def calc(self, n, r):
        # Return nCr mod m
        if n > self._n_max:
            raise Exception('n is larger than n_max')

        if r == 0:
            return 1

        m = self._m
        return self._mod_facts[n] * self._mod_inv_facts[r] * self._mod_inv_facts[n-r] % m


# n^p % mod
def modPower(mod, n, p):
    ret = 1
    for i in range(p):
        ret = ret*n % mod
    return ret


# Expected value of the number of all games when M-th non-even game occurs
def expectedGameNum(mod, C, M):
    return M*100*pow(100-C, mod-2, mod) % mod


# Probability of ending game when M-th non-even game occurs
def probEndGame(mod, modcomb, A, B, M, N):
    winA = modcomb.calc(M-1, N-1) * \
            (modPower(mod, A, N) * pow(modPower(mod, 100, N), mod-2, mod) % mod) * \
            (modPower(mod, B, M-N) * pow(modPower(mod, 100, M-N), mod-2, mod) % mod) % mod
    winB = modcomb.calc(M-1, N-1) * \
            (modPower(mod, B, N) * pow(modPower(mod, 100, N), mod-2, mod) % mod) * \
            (modPower(mod, A, M-N) * pow(modPower(mod, 100, M-N), mod-2, mod) % mod) % mod
    return (winA + winB) % mod

  def main():
    MOD = 10**9+7
    N, A, B, C = map(int, input().split())
    N_MAX = 100000
    modcomb = ModComb(N_MAX*2, MOD)
    ans = 0
    for i in range(N, 2*N):
        ans += expectedGameNum(MOD, C, i) * probEndGame(MOD, modcomb, A, B, i, N) % MOD
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
mod = 10 ** 9 + 7

W = [1] * (2 * N)
for i in range(2 * N - 1):
	W[i + 1] = ((w + 1) * W[i]) % mod

def rev(a):
	return pow(a, mod - 2, mod)

def comb(a, b):
	return (W[a] * rev(W[b]) * rev(W[a - b])) % mod

def f(a, b):
	res = 0
	for i in range(N):
		res += ((i + N) * com[i] * pow(b, i, mod)) % mod
		res %= mod
	return (pow(a, N, mod) * res) % mod

a = A * rev(A + B)
b = B * rev(A + B)
c = C * rev(100)
com = [comb(i + N - 1, i) for i in range(N)]

w_a = f(a, b)
w_b = f(b, a)
ans = ((w_a + w_b) * rev(1 - c)) % mod
print(ans)

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())

mod = 10 ** 9 + 7
table = [1] * (2 * N + 1)
for i in range(2*N):
    table[i+1] = table[i] * (i+1) % mod


def invmod(a, mod):
    # mod が素数の時に成立する
    return pow(a, mod - 2, mod)


def cmb(n, r):
    return (table[n] * invmod(table[r]) * invmod(table[n - r])) % mod


def solve(a, b):
    res = 0
    for i in range(N):
        res += ((i + N) * cmbs[i] * pow(b, i, mod)) % mod
        res %= mod
    return (pow(a, N, mod) * res) % mod


a = A * invmod(A + B)
b = B * invmod(A + B)
c = C * invmod(100)
cmbs = [cmb(i + N - 1, i) for i in range(N)]

Ea = solve(a, b)
Eb = solve(b, a)
ans = ((Ea + Eb) * invmod(1 - c)) % mod
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

def rev_mod(num):
	pow_num = MOD - 2
	ret = 1
	while pow_num > 0:
        ret *= num * (pow_num % 2)
        ret %= MOD
		num *= num
		num %= MOD
		pow_num /= 2
	return ret

N, A, B, C = map(int, raw_input().split())

ans = 0

for loop in range(2):
    cnt = 0
    P = 1
    comb = 1
    PA = (A * rev_mod(A + B)) % MOD
    PB = (B * rev_mod(A + B)) % MOD
    PCm = 100 * rev_mod(100 - C) % MOD
    for i in range(N - 1):
        P *= PA
        P %= MOD

    for i in range(N):
        ans += PCm * (N + i) % MOD * P % MOD * comb % MOD * PA
        ans %= MOD
        P *= PB
        P %= MOD
        comb *= N + i
        comb %= MOD
        comb *= rev_mod(i + 1)
        comb %= MOD
    A, B = B, A
print ans

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

# factorials
_nfact = 10 ** 5 + 10
facts = [0] * _nfact
ifacts = [0] * _nfact
facts[0] = 1
ifacts[0] = 1
for i in range(1, _nfact):
    facts[i] = (facts[i - 1] * i) % MOD
    ifacts[i] = INV(facts[i])

def binomial(m, n):
    return facts[m] * ifacts[n] * ifacts[m - n]

def main():
    N, A, B, C = LI()
    D = DIV(100, 100 - C)
    ans = 0
    for m in range(N, 2 * N):
        x = binomial(m - 1, N - 1) * DIV(
            POW(A, N) * POW(B, m - N) + POW(A, m - N) * POW(B, N),
            POW(A + B, m)) % MOD
        y = (m * D) % MOD
        ans += (x * y) % MOD
    return ans

print(main())

Submission status: Runtime Error

Solution code:
,a,b,c = map(int, input().split())
p = (10**9) +7

def makefactable(MAX,p):
    fac = [1,1]+[0]*(MAX-2)
    inv = [0,1]+[0]*(MAX-2)
    finv = [1,1]+[0]*(MAX-2)
    for i in range(2,MAX):
        fac[i] = fac[i - 1] * i % p
        inv[i] = p - inv[p%i] * (p // i) % p
        finv[i] = finv[i - 1] * inv[i] % p
    return fac,inv,finv
fac = makefactable(n*2+10,p)[0]
inv = makefactable(n*2+10,p)[1]
finv= makefactable(n*2+10,p)[2]

def makecontable():
    l = [1]*n
    for i in range(n):
        l[i] = (fac[n+i]*finv[i]*finv[n])%p
    return l
combo = makecontable()


def power_func(a,b,p):
  """a^b mod p を求める"""
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p ))%p


cnt = 0
for k in range(n):
    ans = 1
    ans *= combo[k]
    ans *= power_func(a+b,n-k,p)
    ans *= power_func(a*b,k,p)
    ans *= (power_func(a,n-k,p)+power_func(b,n-k,p))%p
    cnt = (cnt + ans) %p
    
P = (100*n*cnt)%p
Q = (100-c)*power_func(a+b,2*n,p)

print(P*(power_func(Q,p-2,p)) % p)

Submission status: Runtime Error

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd

def lcm(n,m):
    return int(n*m/gcd(n,m))

import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace

from queue import PriorityQueue as pq

# from itertools import accumulate
# from collections import deque
import random

def reduce(p, q):
    common = fractions.gcd(p, q)
    return (p//common , q//common )

def main():
    n,a,b,c = map(int, input().split())
    q=max(a,b)
    eprint("q: ",q)
    a=Fraction(a,100)
    b=Fraction(b,100)
    ###
    # exception=
    
    numerator=n*(a**n)*b + n*a*(b**n) +2*n*(a**n)*(b**n) - n*(a**n)*(b**2) -n*(a**2)*(b**n) -n*(a**2)*(b**n) - n*(a**(n+1))*(b**k) - k*(a**k)(b**(k+1))
    denominator=(1-a)(1-b)

    ##
    n=numerator
    q=denominator
    temp=reduce(n,q)
    n=temp[0]
    eprint("n:",n)
    q=temp[1]
    eprint("q: ",q)


    
    ##
    print(lcm(n,q))
    



    
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
#v = list(map(int,input().split()))

bign = 1000000007

ab = a+b

"""
nlist = []
k = 2*n -1
counter = 0
while(k):
    nlist.append(k%2)
    k = k // 2

tmp = ab
tmplist = []
for _ in range(17):
    tmplist.append(tmp)
    tmp = (tmp**2) % bign
mo = 1
for t,t2 in zip(tmplist,nlist):
    if t2:
        mo = (mo*t) % bign

q = mo % bign
"""


if not a:
    if c:
        print(n*100/c % bign)
    else:
        print(n)
elif not b:
    if c:
        print(n*100/c % bign)
    else:
        print(n)

def power(tmp,nt):
    ntlist = []
    k = nt
    while(k):
        ntlist.append(k%2)
        k = k // 2


    tmplist = []
    for _ in range(17):
        tmplist.append(tmp)
        tmp = (tmp**2) % bign
    mo = 1
    for t,t2 in zip(tmplist,ntlist):
        if t2:
            mo = (mo*t) % bign

    return mo % bign

q = power(ab,2*n-1)
an = power(a,n)
bn = power(b,n)


def cmb(asd, r, mod):
    if ( r<0 or r>asd ):
        return 0
    r = min(r, asd-r)
    return g1[asd] * g2[r] * g2[asd-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



ans = 0
at = 1
bt = 1
for i in range(n):
    ans += an*bt*cmb(n-1+i,i,bign)*(n+i)*power(ab,n-1-i) % bign
    bt = bt * b % bign
    ans %= bign

for i in range(n):
    ans += bn*at*cmb(n-1+i,i,bign)*(n+i)*power(ab,n-1-i) % bign
    at = at * a % bign
    ans %= bign


if c:
    q = q*c %bign
    ans = ans * 100 % bign


nlist = []
k = bign-2
counter = 0
while(k):
    nlist.append(k%2)
    k = k // 2


qlist = []
for _ in range(40):
    qlist.append(q)
    q = (q**2) % bign
mo = 1
for t,t2 in zip(qlist,nlist):
    if t2:
        mo = (mo*t) % bign

q = mo % bign

print(q*ans %bign)

Submission status: Runtime Error

Solution code:
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a


def reduce(p, q):
    common = gcd(p, q)
    return (p // common, q // common)

n,a,b,c = map(int,input().split())
d = a + b
a1 = a ** n
b1 = b ** n
e = 0
for i in range(n,2*n):
    m1 = i - n
    m2 = n - m1
    o = cmb(i-1,m1) 
    a2 = a ** m1
    b2 = a ** m1
    x1 = a1 * b2 * (d ** m2) * o
    x2 = a2 * b1 * (d ** m2) * o
    e += x1 + x1

p = e * 100
q = (d ** (2*n)) * c
u = reduce(p,q)
p = u[0]
q = u[1] % (10**9 +7)
print(p // q)

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
X = (100*N)/A*B

Submission status: Runtime Error

Solution code:
from fractions import Fraction as F
I=lambda:map(int,input().split())
mod=1000000007
N,A,B,C=I()
a=F(A,100); b=F(B,100); c=F(C,100)
f=N*(a**N)/((1-c)**N)/(1-c)*((b**N)/(c**N)-1)/((b/c)-1)
g=N*(b**N)/((1-c)**N)/(1-c)*((a**N)/(c**N)-1)/((a/c)-1)
print(f+g)

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int,input().split())

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

a = float(A / (A + B))
b = float(B / (A + B))
c = float(C / 100)

ev = 0

for i in range(N):
    ev += ((a**N)*(b**i)+(a**i)*(b**N))*cmb(N+i, i)*(N+i)

if c != 0:
    ev = ev / c

flag = 0
j = 1
while flag == 0:
    if (ev * j).is_integer():
        flag = 1
        Q = j
        P = int((Q * ev) % (10**9+7))
    j += 1

R = 1
flag = 0
while flag == 0:
    if (R*Q) % (10**9+7) == P:
        flag = 1
    else:
        R += 1
    
print(R)

Submission status: Runtime Error

Solution code:

N,A,B,C = list(map(int,input().split()))

P = N*((A+B)**2)*C
Q = A*B*100

def gcd(a, b):
    if b == 0:
      return a
    return gcd(b,a%b)

g=gcd(P,Q)

P=P//g
Q=Q//g

for i in range(Q):
    if (P+ ((10**9) +7)*i)%Q==0:
        R = (P+ ((10**9) +7)*i)//Q
        break
print(R)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

// 変数ダンプ先。coutかcerr
#define DUMPOUT cerr

// 提出時はコメントアウト
#define DEBUG_

// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; cin >> x; return x;}
inline ll IN() {ll x; cin >> x; return x;}
inline vs split(const string& t, char c) {vs v; stringstream s(t); string b; while(getline(s, b, c)) v.eb(b); return v;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template <typename T, typename S> inline void print(const pair<T, S>& p) {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << '\n';}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v) {for (auto&& p : v) print(p);}
template <typename T> inline void print(const vector<T>& v, string s = " ") {REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
// clang-format on
/* }}} */

#ifdef DEBUG_
#define DEB
#else
// DEB と打つとデバッグ時以外はコメントアウトになる
#define DEB /##/
#endif

// 変数ダンプ用マクロ。デバッグ時以外は消滅する
// 引数はいくつでもどんな型でも可（ストリーム出力演算子があればOK）
#define dump(...) DEB DUMPOUT<<"  "; \
DUMPOUT<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl; \
DUMPOUT<<"    "; \
dump_func(__VA_ARGS__)

// デバッグ用変数ダンプ関数
void dump_func() {
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&&... tail)
{
    DUMPOUT << head;
    if (sizeof...(Tail) == 0) {
        DUMPOUT << " ";
    }
    else {
        DUMPOUT << ", ";
    }
    dump_func(std::move(tail)...);
}

// vector出力
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
    os << "{";
    for (int i = 0; i<vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}

ll mod = 1000000007;

ll modplus(ll x, ll y) {
    return (x + y) % mod;
}

ll modminus(ll x, ll y) {
    return (x - y + mod) % mod;
}

ll multiply(ll x, ll y) {
    return (x % mod) * (y % mod) % mod;
}

ll power(ll x, ll y) {
    if (y == 0) {
        return 1;
    } else if (y == 1) {
        return x % mod;
    } else if (y % 2 == 0) {
        ll p = power(x, y / 2);
        return p * p % mod;
    } else {
        ll p = power (x, y / 2);
        return (p * p) % mod * (x % mod) % mod;
    }
}

ll divide(ll x, ll y) {
    return multiply(x, power(y, mod - 2));
}

ll frac[1000001];
ll invfrac[1000001];

void fracinvfrac(ll n) {
    frac[0] = 1;
    for (int i = 1; i <= n; i++) {
        frac[i] = multiply(frac[i - 1], i);
    }
    invfrac[n] = divide(1, frac[n]);
    for (int i = n - 1; i >= 0; i--) {
        invfrac[i] = multiply(invfrac[i + 1], i + 1);
    }
}

ll modcombi(ll x, ll y) {
    if (y == 0) {
        return 1;
    } else {
        return multiply(multiply(frac[x], invfrac[x - y]), invfrac[y]);
    }
}

ll combi(ll x, ll y) {
    ll num = 1;
    for (int i = 1; i <= y; i++) {
        num = num * (x - i + 1) / i;
    }
    return num;
}

signed main() {
    ll N, A, B, C; cin >> N >> A >> B >> C;

    fracinvfrac(N*2);

    if (A == 0 || B == 0) {
        ll P = N * 100;
        ll Q = 100 - C;
        ll R = divide(P, Q);
        print(R);
    } else {
        // A, B only
        ll AB = A + B;
        ll P, Q, PA, QA, PB, QB;
        ll p, q, an, bn, abn, ai, bi, abi;
        an = power(A, N);
        bn = power(B, N);
        abn = power(AB, N);

        bi = 1;
        abi = 1;
        P = -1;
        Q = -1;
        REP(i, N) {
            p = modcombi(N - 1 + i, i);
            p = multiply(p, an);
            p = multiply(p, bi);
            p = multiply(p, N + i);
            q = multiply(abn, abi);
            bi = multiply(bi, B);
            abi = multiply(abi, AB);
            // if (C != 0) {
            //     p = multiply(p, 100);
            //     q = multiply(q, 100 - C);
            // }
            if (P == -1 && Q == -1) {
                P = p;
                Q = q;
            } else {
                P = modplus(multiply(P, q), multiply(p, Q));
                Q = multiply(Q, q);
            }
        }
        PA = P;
        QA = Q;

        ai = 1;
        abi = 1;
        P = -1;
        Q = -1;
        REP(i, N) {
            p = modcombi(N - 1 + i, i);
            p = multiply(p, ai);
            p = multiply(p, bn);
            p = multiply(p, N + i);
            q = multiply(abn, abi);
            ai = multiply(ai, A);
            abi = multiply(abi, AB);
            // if (C != 0) {
            //     p = multiply(p, 100);
            //     q = multiply(q, 100 - C);
            // }
            if (P == -1 && Q == -1) {
                P = p;
                Q = q;
            } else {
                P = modplus(multiply(P, q), multiply(p, Q));
                Q = multiply(Q, q);
            }
        }
        PB = P;
        QB = Q;
        P = modplus(multiply(PA, QB), multiply(PB, QA));
        Q = multiply(QA, QB);
        if (C != 0) {
            P = multiply(P, 100);
            Q = multiply(Q, 100 - C);
        }
        ll R = divide(P, Q);
        print(R);
    }

    return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々

Submission status: Runtime Error

Solution code:
from fractions import gcd

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


n,a,b,c=map(int,input().split())
p=0
A=a/100
B=b/100
C=c/100
s=0
b=0

for i in range(n):
    if i == 0:
        p+=(A**n)*n
        p+=(B**n)*n
    elif i !=0:
        p+=(A**n)*(B**i)*cmb(n+i-1,i)*(n+i)
        p+=(B**n)*(A**i)*cmb(n+i-1,i)*(n+i)
    
p+=100*c/(100-c)**2
s=p*100**(2*n-1)*(100-c)**2
b=100**(2*n-1)*(100-c)**2
g=gcd(s,b)
ans=(s/g*b/g)%(10**9+7)

print(int(ans))