Under construction

Still being built. Things may change

Ability

Time limit2sMemory limit512 MB

Summary
Compute the expected damage of one attack where abilities are tried in a uniformly random order without replacement until one fires, and output the fraction modulo 1e9+7.
Level

Medium7 of 10

Topics
Probability, Math, Combinatorics, Dynamic programming
Solved
No attempts yet

Problem

A player fights monsters in a web game and has all NN attack abilities equipped at once. The abilities are numbered 1 through NN.

Ability ii has a trigger probability pip_i and a damage value did_i. When the player issues a trigger command to ability ii, the ability fires with probability pip_i and deals did_i damage to the opponent, and with probability 1−pi1 - p_i it does not fire and nothing happens.

One attack opportunity runs like this.

  1. Choose one of the equipped abilities. Every ability is equally likely to be chosen.
  2. Issue a trigger command to the chosen ability.
  3. If the ability fires, the attack opportunity is spent and the process ends.
  4. If it does not fire, choose one of the abilities that has not received a command yet, again with equal probability, then go back to step 2.
  5. If every ability has received a command and none of them fired, the attack opportunity is spent with no damage.

Given the trigger probability and the damage of all NN abilities, compute the expected damage dealt in one attack opportunity.

Input

The first line contains the number of abilities NN (1≤N≤50001 \le N \le 5000).

Line ii of the next NN lines contains two integers pip_i and did_i (1≤pi,di≤1091 \le p_i, d_i \le 10^9) separated by a space. Ability ii fires with probability pi/109p_i / 10^9 and deals did_i damage when it fires.

Output

Print the expected damage dealt in one attack opportunity. Write the expected value as an irreducible fraction a/ba/b and print (a×b−1) mod (109+7)(a \times b^{-1}) \bmod (10^9 + 7) instead, where b−1b^{-1} is the multiplicative inverse of bb modulo 109+710^9 + 7. This value exists for every input given in this problem.

Examples8

  1. Example 1

    Input
    1
    500000000 10
    
    Expected output
    5
    
  2. Example 2

    Input
    9
    1 9
    2 8
    3 7
    4 6
    5 5
    6 4
    7 3
    8 2
    9 1
    
    Expected output
    93531355
    
  3. Example 3

    Input
    1
    1000000000 1000000000
    
    Expected output
    1000000000
    
  4. Example 4

    Input
    1
    1 1
    
    Expected output
    857142863
    
  5. Example 5

    Input
    3
    1000000000 1
    1000000000 2
    1000000000 3
    
    Expected output
    2
    
  6. Example 6

    Input
    2
    1 1000000000
    1000000000 1
    
    Expected output
    571428577
    
  7. Example 7

    Input
    5
    2 3
    4 5
    6 7
    8 9
    10 11
    
    Expected output
    55532270
    
  8. Example 8

    Input
    6
    999999999 1000000000
    1000000000 999999999
    500000000 2
    1 1
    123456789 987654321
    999999998 3
    
    Expected output
    326678928