Mukjjippa

시간 제한2초메모리 제한1024 MB

요약
각 턴에서 두 선수의 선택 확률이 주어질 때, mukjjippa 게임에서 A가 이길 확률을 구한다.
난이도

보통10점 중 6점

유형
확률, 동적 계획법, 수학, 구현
정답자
아직 제출이 없습니다

문제

Two players A and B are playing a game called mukjjippa.

The game consists of several turns.

At the ii-th turn (1≤i≤n1\le i\le n):

  • Each player chooses exactly one from R,S,P\\{\mathrm R,\mathrm S,\mathrm P\\} (meaning rock, scissors, and paper, respectively).
  • Let X_iX\_i and Y_iY\_i be the choices of A and B, respectively.
  • If (X_i,Y_i)∈(R,S),(S,P),(P,R)(X\_i,Y\_i)\in\\{(\mathrm R,\mathrm S) ,(\mathrm S,\mathrm P) ,(\mathrm P,\mathrm R)\\}, then A becomes an attacker for the (i+1)(i+1)-th turn and the game continues.
  • Otherwise, if (X_i,Y_i)∈(R,P),(S,R),(P,S)(X\_i,Y\_i)\in\\{(\mathrm R,\mathrm P) ,(\mathrm S,\mathrm R) ,(\mathrm P,\mathrm S)\\}, then B becomes an attacker for the (i+1)(i+1)-th turn and the game continues.
  • Otherwise, if there is an attacker for the ii-th turn, then the attacker becomes a winner and the game ends.
  • Otherwise, there is no attacker for the (i+1)(i+1)-th turn and the game continues.

Note that there is no attacker for the first turn.

If the game does not end until the beginning of the (n+1)(n+1)-th turn, nobody is a winner.

The probability distribution of each choice is given. All choices are independent.

Find the probability that A wins.

입력

The first line contains an integer nn.

The ii-th of the next nn lines contains three integers r_ir\_i, s_is\_i, and p_ip\_i. This means that the probabilities that X_iX\_i is R\mathrm R, S\mathrm S, and P\mathrm P are r_ir_i+s_i+p_i\frac{r\_i}{r\_i+s\_i+p\_i}, s_ir_i+s_i+p_i\frac{s\_i}{r\_i+s\_i+p\_i}, and p_ir_i+s_i+p_i\frac{p\_i}{r\_i+s\_i+p\_i}, respectively.

The ii-th of the next nn lines contains three integers r_i′r\_i', s_i′s\_i', and p_i′p\_i'. This means that the probabilities that Y_iY\_i is R\mathrm R, S\mathrm S, and P\mathrm P are r_i′r_i′+s_i′+p_i′\frac{r\_i'}{r\_i'+s\_i'+p\_i'}, s_i′r_i′+s_i′+p_i′\frac{s\_i'}{r\_i'+s\_i'+p\_i'}, and p_i′r_i′+s_i′+p_i′\frac{p\_i'}{r\_i'+s\_i'+p\_i'}, respectively.

출력

Let xy\frac{x}{y} be the probability that A wins, where xx and yy are coprime integers, and x≥0x\ge 0 and y>0y>0.

Print the integer zz such that yz≡x(mod998,244,353)yz\equiv x\pmod{998\\, 244\\, 353} and 0≤z<998,244,3530\le z<998\\, 244\\, 353.

It can be proved that such an integer zz always exists and is uniquely determined, under the constraints of this problem.

제한

  • 1≤n≤2×1051\le n\le 2\times 10^5
  • 0≤r_i,s_i,p_i≤1060\le r\_i,s\_i,p\_i\le 10^6 (1≤i≤n1\le i\le n)
  • r_i+s_i+p_i>0r\_i+s\_i+p\_i>0 (1≤i≤n1\le i\le n)
  • 0≤r_i′,s_i′,p_i′≤1060\le r\_i',s\_i',p\_i'\le 10^6 (1≤i≤n1\le i\le n)
  • r_i′+s_i′+p_i′>0r\_i'+s\_i'+p\_i'>0 (1≤i≤n1\le i\le n)

예제2

  1. 예제 1

    입력
    2
    1 0 0
    0 1 0
    0 1 0
    0 1 0
    
    예상 출력
    1
    
  2. 예제 2

    입력
    2
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    
    예상 출력
    443664157