Under construction

Still being built. Things may change

Array Interval Sum Game

Interview

Time limit1.5sMemory limit512 MB

Summary
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 AA of length nn made of nn distinct integers. The elements are A[1],…,A[n]A[1], \dots, A[n].
  • Next, choose mm pairs of integers (x1,y1),…,(xm,ym)(x_1, y_1), \dots, (x_m, y_m) that describe intervals of AA. Each pair must satisfy 1≤xi≤yi≤n1 \le x_i \le y_i \le n.
  • The interval sum is defined as S(A,x,y)=∑j=1m(∑i=xjyjA[i])S(A, x, y) = \sum_{j=1}^{m} \left( \sum_{i=x_j}^{y_j} A[i] \right)
  • The goal is to rearrange the elements of AA in any order so that S(A,x,y)S(A, x, y) is as large as possible. Since there are nn elements, there are n!n! possible rearrangements.

For example, let n=3n = 3, m=2m = 2, A=[10,100,1000]A = [10, 100, 1000], x=[1,2]x = [1, 2], and y=[2,2]y = [2, 2]. The largest possible value is 21002100, and the rearrangement [100,1000,10][100, 1000, 10] is the only one that reaches it.

Another example: let n=2n = 2, m=1m = 1, A=[20,22]A = [20, 22], x=[1]x = [1], and y=[2]y = [2]. The largest possible value is 4242, and both rearrangements reach it.

Given AA, xx, and yy, find the largest value of S(A,x,y)S(A, x, y) that Alice can reach by rearranging AA, and count how many rearrangements reach that value.

Input

The first line contains the number of test cases TT.

Each test case starts with a line containing nn and mm, separated by a space. The second line contains the nn elements of AA, separated by spaces. Each of the next mm lines contains two integers xix_i and yiy_i, separated by a space.

Output

For each test case, print the maximum value of S(A,x,y)S(A, x, y) and the number of rearrangements that reach it, separated by a space. Print the count modulo 109+710^9+7.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤n≤50 0001 \le n \le 50\,000
  • 1≤m≤200 0001 \le m \le 200\,000
  • 1≤1 \le each element of A≤108A \le 10^8
  • The elements of AA are distinct
  • For every integer ii with 1≤i≤m1 \le i \le m, 1≤xi≤yi≤n1 \le x_i \le y_i \le n

Examples1

  1. Example 1

    Input
    6
    3 2
    10 100 1000
    1 2
    2 2
    2 1
    20 22
    1 2
    5 3
    1 3 5 7 9
    1 2
    2 3
    3 4
    6 1
    1 2 3 100 200 300
    1 2
    7 7
    100000000 99999999 99999998 99999997 99999996 99999995 99999994
    1 7
    2 7
    3 7
    4 7
    5 7
    6 7
    7 7
    13 1
    1 2 3 4 5 6 7 8 9 10 11 12 13
    1 13
    
    Expected output
    2100 1
    42 2
    40 4
    500 48
    2799999944 1
    91 227020758