Under construction

Still being built. Things may change

Tactics

Time limit2sMemory limit1024 MB

Summary
Find the probability and average of X squared when the final score X of N random tactics is positive, and the same when it is negative, modulo 998244353.
Level

Medium7 of 10

Topics
Brute force, Sorting, Two pointers, Probability
Solved
No attempts yet

Problem

Ray is a player of the character collection game 'Blue Storage'. Today Ray is again practicing boss time attacks to get a high ranking. To defeat a boss faster than other players, one has to pull many strong characters, but Ray is a broke college student and could not pull many. Instead, Ray tries to set a good record by combining tactics, various strategies that can attack a boss efficiently.

Ray searched the game community overnight and found NN tactics in total. The ii-th tactic succeeds with probability piqi\dfrac{p_i}{q_i} and fails with probability 1−piqi1-\dfrac{p_i}{q_i}. Tactics are executed in order. Ray's boss time attack score increases by GiG_i if the ii-th tactic succeeds and decreases by LiL_i if it fails.

Let XX be Ray's score after all tactics are executed. If XX is positive, Ray gains X2X^{2} units of inner peace and plays the game harder. If XX is negative, Ray takes X2X^{2} units of stress and becomes more likely to quit the game. Compute the inner peace and stress information Ray gets.

Input

The first line contains the number of tactics NN. (1≤N≤401 \leq N \leq 40)

From the second line onward, the next NN lines each contain four integers pip_i, qiq_i, GiG_i, and LiL_i, separated by spaces. pip_i and qiq_i give the success probability of the ii-th tactic, GiG_i is the points gained on success, and LiL_i is the points lost on failure. (1≤pi<qi≤1071 \leq p_i < q_i \leq 10^{7}, 1≤Gi,Li≤1071 \leq G_i, L_i \leq 10^{7})

All given numbers are integers. The tactics are given in the order in which they are executed.

Output

On the first line, print the probability P1 mod 998244353P_1 \bmod 998244353 that Ray gets inner peace after all tactics are executed, followed by the average E1 mod 998244353E_1 \bmod 998244353 of the inner peace value when Ray gets it.

On the second line, print the probability P2 mod 998244353P_2 \bmod 998244353 that Ray gets stress after all tactics are executed, followed by the average E2 mod 998244353E_2 \bmod 998244353 of the stress value when Ray gets it.

Only inputs where P1 mod 998244353P_1 \bmod 998244353 and P2 mod 998244353P_2 \bmod 998244353 are not 00 are given.

For a fraction pq\dfrac{p}{q}, pq mod 998244353\dfrac{p}{q} \bmod 998244353 is the integer uu with 0≤u<9982443530 \le u < 998244353 that satisfies qu≡p(mod998244353)qu \equiv p \pmod{998244353}. The integer uu is guaranteed to exist and be unique for every value to print.

Examples1

  1. Example 1

    Input
    2
    1 2 10 6
    2 5 15 10
    
    Expected output
    199648871 353
    898419918 256