logo AlgoBeat OnlineJudge
登录 注册

#215443. [MCO 2023] Segment Union

内存限制:512 MiB 时间限制:5000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

There are positive integers and another positive integers .

Let be a permutation of . Initially, the entire number line is white. For each , the segment is colored black. is then defined as the total length of black segments on the number line. For example, if is colored black, then the total length of black segments is .

Find the sum of over all permutations of , modulo .

输入格式

The first line of input contains a single integer ().

The second line of input contains space-separated integers ().

The third line of input contains space-separated integers ().

输出格式

Output a single integer, the sum of over all permutations of , modulo .

样例

样例输入 1

3
2 6 15
1 2 4

样例输出 1

78

样例输入 2

1
1
7

样例输出 2

14

样例输入 3

4
7 2 7 2
3 2 1 2

样例输出 3

240

样例输入 4

7
1 1 2 9 17 26 30
4 4 4 4 4 4 4

样例输出 4

181440

样例输入 5

11
257869734 -413759255 671386528 312442221 -479133479 837936940 -775252592 -785229024 -306462979 685409332 62181930
987323333 202379759 242380132 464003610 240120482 288801746 7692451 552912477 795257073 629515685 667287542

样例输出 5

862900292

样例输入 6

9
0 0 -2000 396 727 999 999 1300 2000
26 268 268 396 561 604 883 998 999

样例输出 6

616426169

数据范围与提示

Note

Sample 1: There are permutations of length 3. Let be the permutation.

  • : the segments are , , , total length = .
  • : the segments are , , , total length = .
  • : the segments are , , , total length = .
  • : the segments are , , , total length = .
  • : the segments are , , , total length = .
  • : the segments are , , , total length = .

The answer is .

Sample 2: There is only one permutation, and the only segment is . The answer is .

Sample 3: Note that there may be duplicate values. Different permutations may create the same sequence, and you should still count them multiple times (as though as they are different).

Sample 4: This fits the constraints of Subtask 1.

Sample 5: Remember to output the answer modulo .

Scoring

Subtask 1 ( points): All are equal, i.e. for all ()

Subtask 2 ( points): ,

Subtask 3 ( points): ,

Subtask 4 ( points):

Subtask 5 ( points):

Subtask 6 ( points): No additional constraints