Expected Beauty

시간 제한1초메모리 제한2048 MB

요약
각 원소를 주어진 구간에서 균등하게 뽑을 때, 인접한 같은 값을 지워 얻는 점수의 최댓값을 제곱한 값의 기댓값을 구한다.
난이도

어려움10점 중 9점

유형
동적 계획법, 조합론, 수학, 스택
정답자
아직 제출이 없습니다

문제

Morgan the robot has an array AA of size NN, indexed from 11 to NN. The value of each element in AA is randomly generated; A_iA\_i can be any integer from L_iL\_i to R_iR\_i (inclusive) with equal probability.

Morgan defines the beauty of AA as follows. First, Morgan has a variable named score that is initialized to 00. An operation on the array aa is as follows:

  • Choose an index ii such that 1≤i<∣a∣1 ≤ i < |a| and a_i=a_i+1a\_i = a\_{i+1}. If no such ii exists, then the operation cannot be performed.
  • Add the value of a_ia\_i to score and remove a_ia\_i from the array.
  • The array aa becomes the concatenation of the remaining elements without changing its order.

The beauty of AA is the maximum value of score2^2 Morgan can possibly get after performing zero or more operations on the array AA.

Since the array is randomly generated, Morgan wonders about the expected beauty of AA. Due to the inefficiency of his algorithm, Morgan asks for your help to calculate the expected value.

입력

Input begins with an integer NN (1≤N≤200,0001 ≤ N ≤ 200\\, 000) representing the size of array AA. Each of the next NN lines contains two integers L_iL\_i R_iR\_i (1≤L_i≤R_i≤1081 ≤ L\_i ≤ R\_i ≤ 10^8).

출력

Let M=998,244,353M = 998\\, 244\\, 353. It can be shown that the expected value can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0 mod Mq \not\equiv 0 \bmod M. Output an integer xx in a single line such that 0≤x<M0 ≤ x < M and x⋅q≡p mod Mx \cdot q \equiv p \bmod M.

예제3

  1. 예제 1

    입력
    3
    1 2
    2 3
    1 3
    
    예상 출력
    831870298
    
  2. 예제 2

    입력
    4
    1 1
    1 1
    2 2
    2 2
    
    예상 출력
    9
    
  3. 예제 3

    입력
    3
    1 2
    3 4
    5 6
    
    예상 출력
    0