Under construction

Still being built. Things may change

Starflower Serenade (Hard)

Time limit2sMemory limit512 MB

Summary
Find the expected number of seeds planted until each flower kind i blooms at least M_i times, printed as R modulo 998244353.
Level

Medium6 of 10

Topics
Dynamic programming, Probability, Math
Solved
No attempts yet

Problem

The king of the winter kingdom decided to gather the most beautiful flowers and make a garland for the queen, who loves flowers. The flowers she likes are special. They bloom only after a magic seed is planted and starlight falls on it. There are NN kinds of flowers that can bloom from a magic seed, and the flower of the ii-th kind blooms with probability pip_{i}. Whether a seed blooms does not depend on other seeds. To make the garland, at least MiM_{i} flowers of each kind ii are needed. Suppose flowers bloom from seeds one at a time until the garland is complete. What is the expected number of seeds needed?

Input

The first line contains an integer NN (1≤N≤61 \leq N \leq 6), the number of kinds of flowers. Each of the next NN lines contains two integers: MiM_{i} (1≤Mi≤101 \leq M_{i} \leq 10), the number of flowers of kind ii that are needed, and aia_{i}, the weight of the blooming probability. 0<ai≤100,0000 < a_{i} \leq 100,000, and the probability pip_{i} that a flower of kind ii blooms is ai/(∑i=1Nai)a_{i} / \left(\sum_{i=1}^{N} a_{i}\right).

Output

Print the expected number of seeds needed to make the garland. Under the given conditions, the value is always a positive rational number. Write it as P/QP/Q, where PP and QQ are coprime positive integers. Print the unique integer RR with 0≤R<9982443530 \leq R < 998244353 such that R×Q≡Pmod  998244353R \times Q \equiv P \mod 998244353.

Examples4

  1. Example 1

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

    Input
    1
    10 1
    
    Expected output
    10
    
  3. Example 3

    Input
    2
    1 1
    1 2
    
    Expected output
    499122180
    
  4. Example 4

    Input
    3
    1 3
    2 2
    3 1
    
    Expected output
    971485877