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)

mod mint {
    use std::fmt;
    use std::ops::{
        Add, AddAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign,
    };

    pub const MOD: i32 = 1_000_000_007;
    pub const ZERO: Mint = Mint { val: 0 };
    pub const ONE: Mint = Mint { val: 1 };
    pub const TWO: Mint = Mint { val: 2 };

    #[derive(Clone, Copy)]
    pub struct Mint {
        val: i32,
    }

    impl Add for Mint {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut v = self;
            v += other;
            v
        }
    }

    impl AddAssign for Mint {
        fn add_assign(&mut self, other: Self) {
            self.val += other.val;
            if self.val >= MOD {
                self.val -= MOD;
            }
        }
    }

    impl Sub for Mint {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut v = self;
            v -= other;
            v
        }
    }

    impl SubAssign for Mint {
        fn sub_assign(&mut self, other: Self) {
            self.val -= other.val;
            if self.val < 0 {
                self.val += MOD;
            }
        }
    }

    impl Mul for Mint {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let mut v = self;
            v *= other;
            v
        }
    }

    impl MulAssign for Mint {
        fn mul_assign(&mut self, other: Self) {
            self.val = ((self.val as i64) * (other.val as i64) % (MOD as i64)) as i32;
        }
    }

    impl Div for Mint {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut v = self;
            v /= other;
            v
        }
    }

    impl DivAssign for Mint {
        fn div_assign(&mut self, other: Self) {
            *self *= other ^ Mint::from(MOD - 2);
        }
    }

    impl BitXor for Mint {
        type Output = Self;
        fn bitxor(mut self, other: Self) -> Self {
            self ^= other;
            self
        }
    }

    impl BitXorAssign for Mint {
        fn bitxor_assign(&mut self, mut other: Self) {
            let mut x = *self;
            *self = 1.into();
            while other.val != 0 {
                if (other.val & 1) == 1 {
                    *self *= x;
                }
                x *= x;
                other.val >>= 1;
            }
        }
    }

    // i32
    impl Add<i32> for Mint {
        type Output = Self;
        fn add(self, other: i32) -> Self {
            self + Mint::from(other)
        }
    }

    impl AddAssign<i32> for Mint {
        fn add_assign(&mut self, other: i32) {
            *self += Mint::from(other);
        }
    }

    impl Sub<i32> for Mint {
        type Output = Self;
        fn sub(self, other: i32) -> Self {
            self - Mint::from(other)
        }
    }

    impl SubAssign<i32> for Mint {
        fn sub_assign(&mut self, other: i32) {
            *self -= Mint::from(other);
        }
    }

    impl Mul<i32> for Mint {
        type Output = Self;
        fn mul(self, other: i32) -> Self {
            self * Mint::from(other)
        }
    }

    impl MulAssign<i32> for Mint {
        fn mul_assign(&mut self, other: i32) {
            *self *= Mint::from(other);
        }
    }

    impl Div<i32> for Mint {
        type Output = Self;
        fn div(self, other: i32) -> Self {
            self / Mint::from(other)
        }
    }

    impl DivAssign<i32> for Mint {
        fn div_assign(&mut self, other: i32) {
            *self /= Mint::from(other);
        }
    }

    impl BitXor<i32> for Mint {
        type Output = Self;
        fn bitxor(self, other: i32) -> Self {
            self ^ Mint::from(other)
        }
    }

    impl BitXorAssign<i32> for Mint {
        fn bitxor_assign(&mut self, other: i32) {
            *self ^= Mint::from(other);
        }
    }

    impl From<i32> for Mint {
        fn from(mut item: i32) -> Self {
            while item >= MOD {
                item -= MOD
            }
            while item < 0 {
                item += MOD
            }
            Mint { val: item }
        }
    }

    // usize
    impl Add<usize> for Mint {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            self + Mint::from(other)
        }
    }

    impl AddAssign<usize> for Mint {
        fn add_assign(&mut self, other: usize) {
            *self += Mint::from(other);
        }
    }

    impl Sub<usize> for Mint {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            self - Mint::from(other)
        }
    }

    impl SubAssign<usize> for Mint {
        fn sub_assign(&mut self, other: usize) {
            *self -= Mint::from(other);
        }
    }

    impl Mul<usize> for Mint {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            self * Mint::from(other)
        }
    }

    impl MulAssign<usize> for Mint {
        fn mul_assign(&mut self, other: usize) {
            *self *= Mint::from(other);
        }
    }

    impl Div<usize> for Mint {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            self / Mint::from(other)
        }
    }

    impl DivAssign<usize> for Mint {
        fn div_assign(&mut self, other: usize) {
            *self /= Mint::from(other);
        }
    }

    impl BitXor<usize> for Mint {
        type Output = Self;
        fn bitxor(self, other: usize) -> Self {
            self ^ Mint::from(other)
        }
    }

    impl BitXorAssign<usize> for Mint {
        fn bitxor_assign(&mut self, other: usize) {
            *self ^= Mint::from(other);
        }
    }

    impl From<usize> for Mint {
        fn from(item: usize) -> Self {
            Mint::from((item % (MOD as usize)) as i32)
        }
    }

    impl fmt::Display for Mint {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", self.val)
        }
    }
}

mod mrat {
    use std::fmt;
    use std::ops::{
        Add, AddAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign,
    };

    use super::mint::Mint;
    pub const MOD: i32 = 1_000_000_007;

    // numerator / denominator
    #[derive(Clone, Copy)]
    pub struct Mrat {
        numerator: Mint,
        denominator: Mint,
    }

    impl Mrat {
        pub fn new(numerator: i32, denominator: i32) -> Self {
            Mrat {
                numerator: numerator.into(),
                denominator: denominator.into(),
            }
        }
    }

    impl Add for Mrat {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut v = self;
            v += other;
            v
        }
    }

    impl AddAssign for Mrat {
        fn add_assign(&mut self, other: Self) {
            self.numerator =
                self.numerator * other.denominator + self.denominator * other.numerator;
            self.denominator *= other.denominator;
        }
    }

    impl Sub for Mrat {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut v = self;
            v -= other;
            v
        }
    }

    impl SubAssign for Mrat {
        fn sub_assign(&mut self, other: Self) {
            self.numerator =
                self.numerator * other.denominator - self.denominator * other.numerator;
            self.denominator *= other.denominator;
        }
    }

    impl Mul for Mrat {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let mut v = self;
            v *= other;
            v
        }
    }

    impl MulAssign for Mrat {
        fn mul_assign(&mut self, other: Self) {
            self.numerator *= other.numerator;
            self.denominator *= other.denominator;
        }
    }

    impl Div for Mrat {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut v = self;
            v /= other;
            v
        }
    }

    impl DivAssign for Mrat {
        fn div_assign(&mut self, other: Self) {
            self.numerator *= other.denominator;
            self.denominator *= other.numerator;
        }
    }

    // i32
    impl Add<i32> for Mrat {
        type Output = Self;
        fn add(self, other: i32) -> Self {
            self + Mrat::from(other)
        }
    }

    impl AddAssign<i32> for Mrat {
        fn add_assign(&mut self, other: i32) {
            *self += Mrat::from(other);
        }
    }

    impl Sub<i32> for Mrat {
        type Output = Self;
        fn sub(self, other: i32) -> Self {
            self - Mrat::from(other)
        }
    }

    impl SubAssign<i32> for Mrat {
        fn sub_assign(&mut self, other: i32) {
            *self -= Mrat::from(other);
        }
    }

    impl Mul<i32> for Mrat {
        type Output = Self;
        fn mul(self, other: i32) -> Self {
            self * Mrat::from(other)
        }
    }

    impl MulAssign<i32> for Mrat {
        fn mul_assign(&mut self, other: i32) {
            *self *= Mrat::from(other);
        }
    }

    impl Div<i32> for Mrat {
        type Output = Self;
        fn div(self, other: i32) -> Self {
            self / Mrat::from(other)
        }
    }

    impl DivAssign<i32> for Mrat {
        fn div_assign(&mut self, other: i32) {
            *self /= Mrat::from(other);
        }
    }

    impl BitXor<i32> for Mrat {
        type Output = Self;
        fn bitxor(mut self, other: i32) -> Self {
            self ^= other;
            self
        }
    }

    impl BitXorAssign<i32> for Mrat {
        fn bitxor_assign(&mut self, other: i32) {
            self.numerator ^= other;
            self.denominator ^= other;
        }
    }

    impl From<i32> for Mrat {
        fn from(item: i32) -> Self {
            Mrat {
                numerator: item.into(),
                denominator: super::mint::ONE,
            }
        }
    }

    // usize
    impl Add<usize> for Mrat {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            self + Mrat::from(other)
        }
    }

    impl AddAssign<usize> for Mrat {
        fn add_assign(&mut self, other: usize) {
            *self += Mrat::from(other);
        }
    }

    impl Sub<usize> for Mrat {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            self - Mrat::from(other)
        }
    }

    impl SubAssign<usize> for Mrat {
        fn sub_assign(&mut self, other: usize) {
            *self -= Mrat::from(other);
        }
    }

    impl Mul<usize> for Mrat {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            self * Mrat::from(other)
        }
    }

    impl MulAssign<usize> for Mrat {
        fn mul_assign(&mut self, other: usize) {
            *self *= Mrat::from(other);
        }
    }

    impl Div<usize> for Mrat {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            self / Mrat::from(other)
        }
    }

    impl DivAssign<usize> for Mrat {
        fn div_assign(&mut self, other: usize) {
            *self /= Mrat::from(other);
        }
    }

    impl BitXor<usize> for Mrat {
        type Output = Self;
        fn bitxor(mut self, other: usize) -> Self {
            self ^= other;
            self
        }
    }

    impl BitXorAssign<usize> for Mrat {
        fn bitxor_assign(&mut self, other: usize) {
            self.numerator ^= other;
            self.denominator ^= other;
        }
    }

    impl From<usize> for Mrat {
        fn from(item: usize) -> Self {
            Mrat {
                numerator: item.into(),
                denominator: super::mint::ONE,
            }
        }
    }

    impl From<Mint> for Mrat {
        fn from(item: Mint) -> Self {
            Mrat {
                numerator: item,
                denominator: super::mint::ONE,
            }
        }
    }

    impl fmt::Display for Mrat {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", self.numerator / self.denominator)
        }
    }
}

// input
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

mod comb {
    use super::mint::Mint;

    use std::ops::{
        Add, AddAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign,
    };

    pub struct CombMod<T> {
        fact: Vec<T>,
        inv_fact: Vec<T>,
    }

    impl<T> CombMod<T>
    where
        T: Mul<Output = T> + Div<Output = T> + Clone + From<i32>,
    {
        pub fn new(n: usize) -> Self {
            let mut fact = vec![0.into(); n + 1];
            let mut inv_fact = vec![0.into(); n + 1];

            let one: T = 1.into();
            fact[0] = one.clone();
            // for i in 1..=n {
            for i in 1..(n + 1) {
                fact[i] = fact[i - 1].clone() * (i as i32).into();
            }
            inv_fact[n] = one.clone() / fact[n].clone();
            for i in (0..n).rev() {
                inv_fact[i] = inv_fact[i + 1].clone() * ((i + 1) as i32).into();
            }

            CombMod {
                fact: fact,
                inv_fact: inv_fact,
            }
        }

        pub fn get_comb(&self, n: usize, r: usize) -> T {
            self.fact[n].clone() * self.inv_fact[n - r].clone() * self.inv_fact[r].clone()
        }
    }
}

fn main() {
    input! {
        n: usize,
        a: i32,
        b: i32,
        c: i32,
    }

    let a = mrat::Mrat::new(a, 100);
    let b = mrat::Mrat::new(b, 100);
    let c = mrat::Mrat::new(c, 100);

    let (a, b) = (a / (a + b), b / (a + b));

    let comb_mod: comb::CombMod<mrat::Mrat> = comb::CombMod::new(2 * n);
    let mut p: mrat::Mrat = 0.into();
    for i in 0..n {
        let v = (a ^ n) * (b ^ i) + (b ^ n) * (a ^ i);
        let v = comb_mod.get_comb(n - 1 + i, n - 1) * v;
        // let u: mrat::Mrat = (n + i).into();
        p = p * (a + b) + v * mrat::Mrat::from(n + i);
    }
    p /= mrat::Mrat::from(1) - c;
    println!("{}", p);
}