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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using E = System.Linq.Enumerable;

partial class Solver
{
    static public long Gcd(long s, long t)
    {
        return t != 0 ? Gcd(t, s % t) : s;
    }

    static private long ExtendedGcd(long a, long b, ref long x, ref long y)
    {
        if (b == 0)
        {
            x = 1; y = 0;
            return a;
        }
        else
        {
            long d = ExtendedGcd(b, a % b, ref y, ref x);
            y -= a / b * x;
            return d;
        }
    }

    static private long Inverse(long a, long mod)
    {
        long x = 0, y = 0;
        if (ExtendedGcd(a, mod, ref x, ref y) == 1)
            return (x + mod) % mod;
        else
            throw new Exception("Invalid inverse " + a + " " + mod);
    }

    static public long ModPow2(long x, long n, long mod)
    {
        long t = x, ret = 1;
        while (n > 0)
        {
            if ((n & 1) != 0)
            {
                ret = (t * ret) % mod;
            }
            t = t * t % mod;
            n >>= 1;
        }
        return ret;
    }
    public void Run()
    {
        const int Mod = 1000000000 + 7;
        var N = ni();
        var A = ni();
        var B = ni();
        var C = ni();


        // N + 2
        // DP(a,b) = (A * DP(a-1,b) + B * DP(a,b-1) + C * (DP(a,b)) / 100
        // 100 * DP(a,b) = A * DP(a-1,b) + B * DP(a,b-1) + C * (DP(a,b)
        // (100 - C) * DP(a,b) =  A * DP(a-1,b) + B * DP(a,b-1)
        //  DP(a,b) =  (A * DP(a-1,b) + B * DP(a,b-1)) / (100 - C)

        // next = 1 + C / 100 * next
        // 100 * next = 100 + C * next
        // (100 - C) * next = 100
        // next = 100 / (100 - C)

        long P = 0;

        var b = new Binomial(100010 * 2, Mod);
        var AN = new long[100010 * 2];
        var BN = new long[100010 * 2];
        var ABN = new long[100010 * 2];
        AN[0] = 1;
        BN[0] = 1;
        ABN[0] = 1;
        for (int i = 1; i < AN.Length; i++)
        {
            AN[i] = (AN[i - 1] * A) % Mod;
            BN[i] = (BN[i - 1] * B) % Mod;
            ABN[i] = (ABN[i - 1] * (A + B)) % Mod;
        }
        for (int i = N; i < 2 * N; i++)
        {
            var M = i - N;
            if (N <= M) throw new Exception();
            // A * N + B * M
            // last must be A
            var c = b[M + N - 1, M];
            // choose * (A / (A+B)) ^ N * (B / (A+B)) ^ M
            // -> choose * A^N * B^M / ((A+B)^(N+M))
            // choose * (B / (A+B)) ^ N * (A / (A+B)) ^ M
            P += (i * c % Mod * AN[N] % Mod * BN[M] % Mod) * Inverse(ABN[N + M], Mod) % Mod;
            P += i * c % Mod * AN[M] % Mod * BN[N] % Mod * Inverse(ABN[N + M], Mod) % Mod;
            P %= Mod;
        }
        P *= 100 * Inverse(100 - C, Mod) % Mod;

        cout.WriteLine(P % Mod);
    }
}

public class Binomial
{
    private readonly long[] factorial;
    private readonly long[] inverseFactorial;
    private readonly long[] inverse;
    private readonly int mod;

    public Binomial(int size, int primeMod)
    {
        size++;
        this.factorial = new long[size];
        this.inverseFactorial = new long[size];
        this.inverse = new long[size];
        this.mod = primeMod;
        Setup(size);
    }

    private void Setup(int size)
    {
        factorial[0] = factorial[1] = 1;
        inverseFactorial[0] = inverseFactorial[1] = 1;
        inverse[1] = 1;
        for (int i = 2; i < size; i++)
        {
            factorial[i] = factorial[i - 1] * i % mod;
            inverse[i] = (mod - (mod / i) * inverse[mod % i] % mod);
            inverseFactorial[i] = inverseFactorial[i - 1] * inverse[i] % mod;
        }
    }

    private long Get(int s, int t)
    {
        if (s < 0 || t < 0 || s < t) return 0;
        if (t == 0 || s == t) return 1;
        if (s >= mod) return Get(s % mod, t % mod) * Get(s / mod, t / mod) % mod; // Lucas' theorem
        return factorial[s] * inverseFactorial[t] % mod * inverseFactorial[s - t] % mod;
    }

    public long this[int s, int t]
    {
        get
        {
            return Get(s, t);
        }
    }

}
// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner
{
    public static void Main(string[] args)
    {
#if LOCAL
        new Solver(Console.In, Console.Out).Run();
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader)
    {
        this.cin = reader;
        this.cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer)
    {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

public class Scanner
{
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In)
    {
    }

    public Scanner(TextReader reader)
    {
        this.Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size)
    {
        var array = new string[size];
        for (int i = 0; i < size; i++) array[i] = Next();
        return array;
    }
    public int[] NextIntArray(int size)
    {
        var array = new int[size];
        for (int i = 0; i < size; i++) array[i] = NextInt();
        return array;
    }

    public long[] NextLongArray(int size)
    {
        var array = new long[size];
        for (int i = 0; i < size; i++) array[i] = NextLong();
        return array;
    }

    public double[] NextDoubleArray(int size)
    {
        var array = new double[size];
        for (int i = 0; i < size; i++) array[i] = NextDouble();
        return array;
    }

    public string Next()
    {
        if (TokenQueue.Count == 0)
        {
            if (!StockTokens()) throw new InvalidOperationException();
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext()
    {
        if (TokenQueue.Count > 0)
            return true;
        return StockTokens();
    }
    static readonly char[] _separator = new[] { ' ' };
    private bool StockTokens()
    {
        while (true)
        {
            var line = Reader.ReadLine();
            if (line == null) return false;
            var tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) continue;
            foreach (var token in tokens)
                TokenQueue.Enqueue(token);
            return true;
        }
    }
}