Background
The Rearrangement Inequality is an inequality that is based on rearranging numbers. If I have two lists of numbers of the same length, x0, x1, x2...xn-1 and y0, y1, y2...yn-1 of the same length, where I am allowed to rearrange the numbers in the list, a way to maximize the sum x0y0+x1y1+x2y2+...+xn-1yn-1 is to sort the 2 lists in non-decreasing order.
Read the Wikipedia article here.
Task
You would write a program that takes input from STDIN or a function that accepts 2 arrays (or related containers) of numbers (which are of the same length).
Assuming you write a function which accepts 2 arrays (a and b), you are going to find the number of ways you can rearrange the numbers in the second array (b) to maximize:
a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+...+a[n-1]*b[n-1]
In this case, if the array b is [10, 21, 22, 33, 34] (indices for clarity),
[10, 21, 22, 33, 34],
[10, 21, 22, 34, 33], (swap the two 3's)
[10, 22, 21, 33, 34] (swap the two 2's)
[10, 22, 21, 34, 33] (swap the two 3's and swap the two 2's)
are considered different arrangements. The original array, itself, also counts as a possible rearrangement if it also maximizes the sum.
For STDIN input, you may assume that the length of the arrays is provided before the arrays (please state so that you use it), or that the arrays are provided on different lines (also please state).
Here are the 4 possible inputs (for convenience):
5 1 1 2 2 2 1 2 2 3 3 (length before arrays)
1 1 2 2 2 1 2 2 3 3 (the 2 arrays, concatenated)
1 1 2 2 2
1 2 2 3 3 (the 2 arrays on different lines)
5
1 1 2 2 2
1 2 2 3 3 (length before arrays and the 2 arrays on different lines)
For output, you are allowed to return the answer (if you write a function) or print the answer to STDOUT. You may choose to output the answer mod 109+7 (from 0 to 109+6) if it is more convenient.
Test Cases (and explanation):
[1 1 2 2 2] [1 2 2 3 3] => 24
The first 2 entries have to be 1 and 2. The last 3 entries are 2, 3 and 3. There are 2 ways to arrange the 2's between the first 2 entries and the last 2 entries. Among the first 2 entries, there are 2 ways to rearrange them. Among the last 2 entries, there are 6 ways to rearrange them.
[1 2 3 4 5] [6 7 8 9 10] => 1
There is only 1 way, which is the arrangement given in the arrays.
[1 1 ... 1 1] [1 1 ... 1 1] (10000 numbers) => 10000! or 531950728
Every possible permutation of the second array is valid.
Dennis' Testcase: Pastebin => 583159312 (mod 1000000007)
Scoring:
This is code-golf, so shortest answer wins.
In case of tie, ties will be broken by time of submission, favouring the earlier submission.
Take note:
The containers may be unsorted.
The integers in the containers may be zero or negative.
The program has to run fast enough (at most an hour) for modestly sized arrays (around 10000 in length).
Inspired by this question on Mathematics Stack Exchange.
[. . .]
plz respond \$\endgroup\$