Array Interval Sum Game
InterviewTime limit1.5sMemory limit512 MB
Rearrange array A to maximize the total of m given interval sums, then count the arrangements that reach that maximum modulo 1e9+7.
- Level
Medium4 of 10
- Topics
- Prefix sum, Sorting, Combinatorics
- Solved
- No attempts yet
Problem
Alice plays an interval sum game with an integer array. The game goes as follows.
- First, create an array of length made of distinct integers. The elements are .
- Next, choose pairs of integers that describe intervals of . Each pair must satisfy .
- The interval sum is defined as
- The goal is to rearrange the elements of in any order so that is as large as possible. Since there are elements, there are possible rearrangements.
For example, let , , , , and . The largest possible value is , and the rearrangement is the only one that reaches it.
Another example: let , , , , and . The largest possible value is , and both rearrangements reach it.
Given , , and , find the largest value of that Alice can reach by rearranging , and count how many rearrangements reach that value.
Input
The first line contains the number of test cases .
Each test case starts with a line containing and , separated by a space. The second line contains the elements of , separated by spaces. Each of the next lines contains two integers and , separated by a space.
Output
For each test case, print the maximum value of and the number of rearrangements that reach it, separated by a space. Print the count modulo .
Constraints
- each element of
- The elements of are distinct
- For every integer with ,