Smaller Averages
Bessie has two arrays of length N (1≤N≤500). The i-th element of the first array is ai (1≤ai≤106) and the i-th element of the second array is bi (1≤bi≤106).
Bessie wants to split both arrays into non-empty subarrays such that the following is true.
- Every element belongs in exactly 1 subarray.
- 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).
- For all 1≤i≤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 109+7. Two ways are considered different if the number of subarrays are different or if some element belongs in a different subarray.
The two valid ways are:
- Split the first array into [1],[2] and the second array into [2],[2].
- Split the first array into [1,2] and the second array into [2,2].
The three valid ways are:
- Split the first array into [1,3],[2] and the second array into [2,2],[2].
- Split the first array into [1,3],[2] and the second array into [2],[2,2].
- Split the first array into [1,3,2] and the second array into [2,2,2].
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].
SCORING:
- Inputs 5-6: N≤10
- Inputs 7-9: N≤80
- Inputs 10-17: N≤300
- Inputs 18-20: N≤500
Problem credits: Alex Liang
Source: USA Computing Olympiad.
Input Format
The first line contains N.
The next line contains a1,a2,...,aN.
The next line contains b1,b2,...,bN.
Output Format
Output the number of ways she can split both arrays into non-empty subarrays while satisfying the constraints modulo 109+7.