#P7496. 干杯!再会!
干杯!再会!
Description
This small shop has regular customers. When the -th customer comes to the shop, they bring a base wine with deliciousness and a seasoning with deliciousness . These customers ask Demi to mix the wine for them. For a base wine with deliciousness and a seasoning with deliciousness , if Demi mixes them together, she can obtain a bottle of fine wine with deliciousness (we consider that a lower deliciousness value means the wine tastes better).
On this day, these customers came to the shop at the same time and wanted Demi to mix the wine. However, Demi drank too much the day before and became confused, which caused her to add the seasonings to the wrong base wines. Fortunately, the customers do not mind. They only want to know, for all possible ways Demi adds the seasonings, what the result is of the sum of the variances of the deliciousness values of the wines they will get, taken modulo . If you can answer their question, they will be very willing to help you pay for the drinks.
Brief statement:
Given and two sequences of length . For a permutation of to , define . Let denote the variance of all elements in sequence (see the variance formula in the hint). Compute:
modulo .
Input Format
The first line contains an integer , as described above.
The second line contains integers; the -th integer is .
The third line contains integers; the -th integer is .
Output Format
Output one integer on one line, the answer.
3
1 2 3
1 2 3
777777785
12
1 3 4 2 3 5 7 3 5 6 8 9
4 3 10 2 5 6 4 8 2 9 12 5
931089600
Hint
Explanation for Sample 1
- .
- .
- .
- .
- .
- .
The total sum is , which is modulo .
Constraints
This problem uses bundled testdata.
- Subtask 1 ( ): .
- Subtask 2 ( ): .
- Subtask 3 ( ): .
- Subtask 4 ( ): .
- Subtask 5 ( ): no special constraints.
For all testdata: .
For a sequence of length , the variance is $\sigma(x)=\sum\limits_{i=1}^n\dfrac{1}{n}(x_i-\bar{x})^2$, where is the average of all elements ().
Translated by ChatGPT 5
京公网安备 11011102002149号