QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#20877. Smaller Averages

Statistics

Bessie has two arrays of length $N$ ($1 \le N \le 500$). The $i$-th element of the first array is $a_i$ ($1 \le a_i \le 10^6$) and the $i$-th element of the second array is $b_i$ ($1 \le b_i \le 10^6$).

Bessie wants to split both arrays into non-empty subarrays such that the following is true.

  1. Every element belongs in exactly 1 subarray.
  2. Both arrays are split into the same number of subarrays. Let the number of subarrays the first and second array are split into be $k$ (i.e. the first array is split into exactly $k$ subarrays and the second array is split into exactly $k$ subarrays).
  3. For all $1 \le i \le k$, the average of the $i$-th subarray on the left of the first array is less than or equal to the average of the $i$-th subarray on the left of the second array.

Count how many ways she can split both arrays into non-empty subarrays while satisfying the constraints modulo $10^9+7$. Two ways are considered different if the number of subarrays are different or if some element belongs in a different subarray.

Input

The first line contains $N$.

The next line contains $a_1,a_2,...,a_N$.

The next line contains $b_1,b_2,...,b_N$.

Output

Output the number of ways she can split both arrays into non-empty subarrays while satisfying the constraints modulo $10^9+7$.

Examples

Input 1

2
1 2
2 2

Output 1

2

The two valid ways are:

  1. Split the first array into $[1],[2]$ and the second array into $[2],[2]$.
  2. Split the first array into $[1,2]$ and the second array into $[2,2]$.

Input 2

3
1 3 2
2 2 2

Output 2

3

The three valid ways are:

  1. Split the first array into $[1,3],[2]$ and the second array into $[2,2],[2]$.
  2. Split the first array into $[1,3],[2]$ and the second array into $[2],[2,2]$.
  3. Split the first array into $[1,3,2]$ and the second array into $[2,2,2]$.

Input 3

5
2 5 1 3 2
2 1 5 2 2

Output 3

1

The only valid way is to split the first array into $[2],[5,1,3],[2]$ and the second array into $[2],[1,5],[2,2]$.

Input 4

7
3 5 2 3 4 4 1
5 3 5 3 3 4 1

Output 4

140

Scoring

  • Inputs 5-6: $N \le 10$
  • Inputs 7-9: $N \le 80$
  • Inputs 10-17: $N \le 300$
  • Inputs 18-20: $N \le 500$

Problem credits: Alex Liang

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.