Value of all Permutations
Difficulty: Hard

You are given an array $A$ of size $N$. You are asked to answer $Q$ queries.  

Each query contains a number $M$.  

For each *distinct* permutation of the array $A$, you need to print the sum of the values returned by the `find` function.

As the sum can be too large, output it modulo $P$, which is a prime and given in input.  

	find(int permutation_A[], int M)
	{
        x = Length(permutation_A)
        sum = 0
        for(i = 0; i < x; i++) {
        	if (permutation_A[i] <= M)
            	sum = sum + permutation_A[i]
            else
            	break
		}
		return sum
    }


**Input Format**  
The first line of input contains $P$.  
The second line of input contains $N$. The next line contains $N$ numbers $A[0] \ldots A[N-1]$ separated by single spaces.  
The next line contains $Q$, the number of queries to follow. Each subsequent line contains one positive integer $M$.  

**Output Format**  
For each query, output as asked above.  

**Constraints**  
$1000000 \le P \le 2000003$  
$1 \le N \le 10^5$  
$1 \le Q \le 10^5$  
$1 \le A[i], M \le 10^9$

**Sample Input**

	2000003
	5
	4 2 3 1 4
	2
	1
	2

**Sample Output**

	12
    45
    
**Explanation**

*Query 1*:  
Consider all permutations. if the first number is greater than 1, then the loop will break in the beginning itself. There are a total of 60 distinct permutations out of which 48 will give 0. The remaining 12 will fetch 1 each from the function. Thus the answer is 12.  


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
###Python 2  
```python  
P = input()

# collect and sort A
N = input()
A = map(int, raw_input().strip().split())
A.sort()

# calculate partial sums S
S = [0]*(N+1)
for i in xrange(N):
    S[i+1] = (S[i] + A[i]) % P

# inverses mod P
inv = [1]*(N+2)
for i in xrange(2,N+2):
    inv[i] = -(P/i) * inv[P%i] % P

# calculate number of distinct permutations
perms = 1
count = 1
for i in xrange(1,N):
    if A[i] != A[i-1]:
        count = 0
    count += 1
    perms *= inv[count] * (i+1)
    perms %= P

# process queries
Q = input()
for cas in xrange(Q):
    M = input()

    # binary search
    h, i = -1, N
    while i - h > 1:
        j = h + i >> 1
        if A[j] <= M:
            h = j
        else:
            i = j

    # at this point, A[i-1] <= M < A[i]
    print S[i] * perms * inv[N-i+1] % P
```