QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#14530. Finding Luggage

Statistics

Mandy and brz got off the plane and prepared to pick up their luggage, but they saw that the area in front of the luggage conveyor belt was crowded. They were both very distressed and wanted to know when someone would leave and make room for them.

Mandy had a sudden inspiration and thought of a problem:

Suppose the conveyor belt is not circular but linear, which can be represented by a coordinate axis. There are $n$ pieces of luggage at positions $a_i$ on the conveyor belt, and $m$ people standing in front of the conveyor belt at positions $b_i$. The conveyor belt moves one unit in the positive direction of the coordinate axis every second, meaning that every second, all luggage positions become $a_i := a_i + 1$. Both people and luggage are numbered starting from 1.

Obviously, for any person, all luggage to their right has already passed by and cannot be their own. Their own luggage can only be to their left, or it might not be on the belt at all. (If coordinate $i < j$, we consider $i$ to be to the left of $j$.) Furthermore, no piece of luggage belongs to more than one person, but it is possible that a piece of luggage does not belong to any of the $m$ people. Each person has at most one piece of luggage.

Now Mandy asks brz: in all possible scenarios, what is the earliest time someone gets their own luggage? brz is even more distressed and has to ask for your help. You need to output the sum of the answers for all scenarios. Of course, the case where no one has their own luggage to their left does not need to be considered. Two scenarios are different if and only if in one scenario a certain person has a piece of luggage while in the other they do not, or if there exists a person whose luggage number is different between the two scenarios.

Input

The first line contains two integers $n, m$ ($1 \le n, m \le 500$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 500$). The third line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le 500$).

We assume that people and luggage are point masses with negligible volume, so it is not guaranteed that $a_i$ are distinct from each other, and the same applies to $b_i$.

Output

Output a single integer representing the answer. Since the answer may be very large, you only need to output the result modulo $998\,244\,353$.

Examples

Input 1

2 2
1 2
3 4

Output 1

11

Input 2

5 5
1 2 3 4 5
2 3 4 5 6

Output 2

272

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.