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

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(dead_code)]

use std::thread;
use std::io::{ self, Read };
use std::fmt;
use std::iter::Iterator;
use std::cmp::*;
use std::collections::*;
use std::str::*;
use std::mem::*;
use std::cell::*;

#[derive(Default)]
struct Input {
    buf: Vec<String>
}

impl Input {
    fn new() -> Input {
        Input { buf: Vec::new() }
    }

    fn read<T: FromStr>(&mut self) -> T 
    where <T as FromStr>::Err: fmt::Debug {
        self.next().unwrap().parse::<T>().unwrap()
    }

    fn read_2<T: FromStr, U: FromStr>(&mut self) -> (T, U)
    where <T as FromStr>::Err: fmt::Debug,
          <U as FromStr>::Err: fmt::Debug {
        (self.read(), self.read())
    }

    fn read_3<T: FromStr, U: FromStr, V: FromStr>(&mut self) -> (T, U, V)
    where <T as FromStr>::Err: fmt::Debug,
          <U as FromStr>::Err: fmt::Debug,
          <V as FromStr>::Err: fmt::Debug {
        (self.read(), self.read(), self.read())
    }

    fn read_bytes(&mut self) -> Vec<u8> {
        self.next().unwrap().into_bytes()
    }

    fn read_n<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where <T as FromStr>::Err: fmt::Debug {
        let mut v = Vec::new();
        for _ in 0..n { v.push(self.read::<T>()); }
        v
    }
}

impl Iterator for Input {
    type Item = String;
    fn next(&mut self) -> Option<String> {
        while self.buf.is_empty() {
            let mut s = String::new();
            let _res = io::stdin().read_line(&mut s);
            self.buf = s.split_whitespace().map(|s| s.to_owned()).collect::<Vec<_>>();
            self.buf.reverse();
        }
        self.buf.pop()
    }
}

const MOD: i64 = 1000000007;

thread_local! {
    static FACT_MEMO: RefCell<[i64; 200000]> = RefCell::new([0; 200000]);
}

fn fact(x: i64) -> i64 {
    FACT_MEMO.with(|rc| {
        let ux = x as usize;
        {
            let memo = rc.borrow();
            if memo[ux] > 0 {
                return memo[ux];
            }
        }
        let y = if x > 0 { x * fact(x - 1) % MOD } else { 1 };
        rc.borrow_mut()[ux] = y;
        y
    })
}
fn pow(a: i64, b: i64) -> i64 {
    if b > 0 {
        let y = pow(a * a % MOD, b >> 1);
        if b & 1 > 0 { a * y % MOD } else { y }
    } else {
        1
    }
}
fn inv(x: i64) -> i64 {
    pow(x, MOD - 2)
}
fn conb(a: i64, b: i64) -> i64 {
    fact(a) * inv(fact(b)) % MOD * inv(fact(a - b)) % MOD
}

fn f(n: i64, a: i64, b: i64, c: i64) -> i64 {
    let mut y = 0;
    let pa = pow(a * inv(a + b) % MOD, n);
    let ec = 100 * inv(100 - c) % MOD;
    for wb in 0..n {
        let pb = pow(b * inv(a + b) % MOD, wb);
        y += (n + wb) * ec % MOD * pa % MOD * pb % MOD * conb(n - 1 + wb, wb) % MOD;
        y %= MOD;
    } 
    y
}

fn run() {
    let mut ip = Input::new();
    let n = ip.read();
    let a = ip.read();
    let b = ip.read();
    let c = ip.read();

    let y = (f(n, a, b, c) + f(n, b, a, c)) % MOD;
    println!("{}", y);
}

fn main() {
    let _ = thread::Builder::new()
        .stack_size(100 * 1024 * 1024)
        .spawn(run).unwrap()
        .join();
}