Under construction

Still being built. Things may change

Smaller Averages

Time limit2sMemory limit1024 MB

Summary
Count the ways to split two length-N arrays into the same number of contiguous parts, where each a-part's average is at most its paired b-part's average, mod 1e9+7.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum
Solved
No attempts yet

Problem

Bessie has two arrays of length NN (1≤N≤5001 \le N \le 500). The ii-th element of the first array is aia_i (1≤ai≤1061 \le a_i \le 10^6), and the ii-th element of the second array is bib_i (1≤bi≤1061 \le b_i \le 10^6).

Bessie wants to split both arrays into non-empty subarrays so that the following holds.

  1. Every element belongs to exactly one subarray.
  2. Both arrays are split into the same number of subarrays. Let this number be kk, so the first array and the second array are each split into exactly kk subarrays.
  3. For every 1≤i≤k1 \le i \le k, the average of the ii-th subarray from the left in the first array is less than or equal to the average of the ii-th subarray from the left in the second array.

Count the ways to split both arrays under these constraints, modulo 109+710^9+7. Two ways differ if the number of subarrays differs or if some element belongs to a different subarray.

Input

The first line contains NN. The next line contains a1,a2,…,aNa_1, a_2, \ldots, a_N. The next line contains b1,b2,…,bNb_1, b_2, \ldots, b_N.

Output

Output the number of ways to split both arrays under these constraints, modulo 109+710^9+7.

Examples4

  1. Example 1

    Input
    2
    1 2
    2 2
    
    Expected output
    2
    
  2. Example 2

    Input
    3
    1 3 2
    2 2 2
    
    Expected output
    3
    
  3. Example 3

    Input
    5
    2 5 1 3 2
    2 1 5 2 2
    
    Expected output
    1
    
  4. Example 4

    Input
    7
    3 5 2 3 4 4 1
    5 3 5 3 3 4 1
    
    Expected output
    140