#P8321. 『JROI-4』沈阳大街 2

    ID: 9157 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2022洛谷原创O2优化排序洛谷月赛

『JROI-4』沈阳大街 2

Problem Description

Given two sequences A,BA, B of length nn, satisfying:

  • 1i<n,AiAi+1\forall 1\le i<n, A_i \ge A_{i+1}.

  • Anmini=1n(Bi)A_n\ge \min\limits_{i=1}^n(B_i).

Let π\pi be a permutation of length nn. Define the value function f(π)f(\pi):

f(π)=i=1nmin(Ai,Bπ(i))f(\pi)=\prod_{i=1}^n\min(A_i,B_{\pi(i)})

All permutations are equally likely. Find the expected value of f(π)f(\pi) modulo 998244353998244353.

That is, compute:

$$\left(\dfrac{1}{n!}\sum_\pi f(\pi)\right) \bmod 998244353$$

Input Format

The first line contains an integer nn.

The second line contains nn integers, denoting AiA_i.

The third line contains nn integers, denoting BiB_i.

Output Format

Output one line containing one integer, the answer.

8
15 14 13 10 9 6 3 2 
2 10 8 2 9 1 10 2 
114102208

Hint

This problem uses bundled testdata.

Subtask ID Score Special Constraints
1 5 1n81\le n\le 8
2 35 1n501\le n\le 50
3 20 Anmaxi=1n(Bi)A_n\ge \max\limits_{i=1}^n(B_i)
4 40 None

For 100%100\% of the data, 1n50001\le n\le 5000, 1Ai,Bi1091\le A_i,B_i\le 10^9.

Translated by ChatGPT 5