Under construction

Still being built. Things may change

Narrower Passageway

Time limit1sMemory limit2048 MB

Summary
Each column is fogged with probability 1/2; find the expected total strength, where each unfogged segment adds min of its two row maxima (0 if equal), modulo 998244353.
Level

Hard8 of 10

Topics
Stack, Probability, Dynamic programming, Array
Solved
No attempts yet

Problem

You are a strategist of The ICPC Kingdom. You received intel that monsters will attack a narrow passageway near the kingdom. The passageway is a grid with 22 rows (numbered from 11 to 22) and NN columns (numbered from 11 to NN). Denote (r,c)(r, c) as the cell in row rr and column cc. Every day, a soldier with power Pr,cP_{r,c} is assigned to protect (r,c)(r, c).

The passageway is very foggy. Within a day, each column has a 50%50\% chance of being covered in fog. If a column is covered in fog, the two soldiers assigned to that column are not deployed that day. Otherwise, the assigned soldiers are deployed.

A connected area [u,v][u, v] (u≤vu \le v) is a maximal set of consecutive columns from uu to vv (inclusive) such that no column in the set is covered in fog. The image below shows an example. The gray cells are covered in fog. There are 44 connected areas: [1,2][1, 2], [4,6][4, 6], [9,9][9, 9], and [11,11][11, 11].

The strength of a connected area [u,v][u, v] is computed as follows. Let m1m_1 and m2m_2 be the maximum power of the soldiers in the first and second rows of the connected area, respectively. Formally, mr=max⁡(Pr,u,Pr,u+1,…,Pr,v)m_r = \max(P_{r,u}, P_{r,u+1}, \dots, P_{r,v}) for r∈{1,2}r \in \{1, 2\}. If m1=m2m_1 = m_2, then the strength is 00. Otherwise, the strength is min⁡(m1,m2)\min(m_1, m_2).

The total strength of the deployment is the sum of the strengths of all connected areas. Determine the expected total strength of the deployment on any single day.

Input

The first line contains an integer NN (1≤N≤100 0001 \le N \le 100\,000).

Each of the next two lines contains NN integers Pr,cP_{r,c} (1≤Pr,c≤200 0001 \le P_{r,c} \le 200\,000).

Output

Let M=998 244 353M = 998\,244\,353. It can be shown that the expected total strength can be expressed as an irreducible fraction xy\frac{x}{y}, where xx and yy are integers and y≢0(modM)y \not\equiv 0 \pmod M. Output an integer kk in a single line such that 0≤k<M0 \le k < M and k⋅y≡x(modM)k \cdot y \equiv x \pmod M.

Examples2

  1. Example 1

    Input
    3
    8 4 5
    5 4 8
    
    Expected output
    249561092
    
  2. Example 2

    Input
    5
    10 20 5 8 5
    5 20 7 5 8
    
    Expected output
    811073541