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 (C++):
###C++  
```cpp  
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<stdlib.h>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
#include<assert.h>
#include<utility>
using namespace std;

typedef long long int ll;

#define For(i,s,e) for(int i=(s);i<=(e);i++)
#define mp(a,b)    make_pair((a),(b))

ll modulo ;

#define mod0 1711276033L        
#define s0 1223522572        //this is the root and it will make it 2^19th modulo mod0

#define mod1 1790967809L        
#define s1 1110378081        //2^19th root of unity modulo mod1

ll power_of_s0[25];
ll power_of_s1[25];

ll M;

int flag ;

void integral_fft_constant()
{
    power_of_s0[0] = s0;
    power_of_s1[0] = s1;
    for(int i=1;i<24;i++)    power_of_s0[i] = ((ll)power_of_s0[i-1]*(ll)power_of_s0[i-1])%mod0;
    for(int i=1;i<24;i++)    power_of_s1[i] = ((ll)power_of_s1[i-1]*(ll)power_of_s1[i-1])%mod1;
}

ll powmod(ll a,ll b,ll m)
{
    ll ans = 1%m;
    while(b){
        if(b&1){
            ans  = ( (ll)ans*(ll)a )%m;
            b--;
        }
        else{
            a = ( (ll)a*(ll)a )%m;
            b>>=1;
        }
    }
    return ans;
}
void integral_fft(ll input[],ll output[] ,int skip ,int size ,int iteration)
{
    if(size==1){
        output[0] = input[0];
        return ;
    }
    integral_fft(input,output,skip*2,size/2,iteration+1);
    integral_fft(input+skip , output+size/2 , skip*2 , size/2 ,iteration+1);
    ll var1 , var2= 1,odd,even,store;
    //var1 is sizeth root of unity.
    if ( flag == 0)    var1 = power_of_s0[iteration];
    else var1 = power_of_s1[iteration];
    for(int k=0;k<size/2;k++){
        even = output[k];
        odd = output[k+size/2];
        store = ((ll)var2*(ll)odd)%M;
        output[k] = (even + store)%M;
        output[k+size/2] = (even - store)%M;    //same as even - store
        if ( output[k+size/2] < 0 ) output[k+size/2] += M;
        var2 = ((ll)var2*(ll)var1)%M;
    }
}
ll CA[(1<<18)+1],CB[(1<<18)+1];

void multiply( ll A[] , ll B[] ,ll result[] ,int y)
{
    integral_fft(A,CA,1,y,0);
    integral_fft(B,CB,1,y,0);
    ll inverse_of_y = powmod(y,M-2,M);
    for(int i=0;i<y;i++)    CA[i] = ( ( (CA[i]*CB[i])%M )*inverse_of_y )%M;    
    integral_fft(CA,result,1,y,0);
    swap(result[0],result[y]);
    reverse(result,result+y+1);    
    //for(int i=0;i<y;i++)    printf("%d ",result[i]);
    //complex_fft(CA,result,1,y,false);
}

void get_poly(ll A[] ,ll B[],ll N)
{
    //A[i] = (N-i-1)! , B[i] = 1/i!
    A[N-1] = 1;
    for(int i=N-2;i>=0;i--)  A[i] = (A[i+1]*(N-1-i))%M;
    B[0]= 1;
    for(int i=1;i<N;i++)    B[i] = (B[i-1]*(ll)powmod(i,M-2,M))%M;
    for(int i=N;i<(1<<18);i++)    A[i]=B[i]=0;
}
ll poly1[1<<18],poly2[1<<18],result1[1<<18],result2[1<<18],fact[100001],result[1<<18];

ll mulmod (ll a,ll b , ll m )
{
    ll ans = 0;
    while( b)
    {
        if ( b&1){
            ans = (ans + a);
            if ( ans > m ) ans -= m ;
            b--;
        }
        else{
            a = a+ a;
            if ( a> m)    a-= m;
            b>>=1;
        }
    }
    return ans;
}

ll CRT(ll rem[] , ll mod[],int t)    //t is the number of pairs of rem and mod
{
    ll M1=1 ;
    for(int i=0;i<t;i++)    M1 = M1*mod[i];
    ll ans = 0;
    for(int i=0;i<t;i++){
        ans = (ans + mulmod(((rem[i]*powmod(M1/mod[i],mod[i]-2,mod[i]))%M1),(M1/mod[i]),M1) )%M1;
    }
    return ans;
}

void mixing()
{
    for(int i=0;i<(1<<18);i++){    
        ll rem[] = { result1[i] , result2[i] };
        ll mod[] = { mod0 , mod1 };
        result[i] = CRT ( rem ,mod,2 )%M;
    }
}

int main()
{
    scanf("%lld",&modulo);
    integral_fft_constant();    
    int t,n,x,index;
    ll factor,constant_factor=1;
    vector<int> A,B;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        A.push_back(x);
    }
    sort(A.begin(),A.end());
    B.push_back(A[0]%modulo);
    for(int i=1;i<n;i++)    B.push_back((B[i-1]+A[i])%modulo);

    M = modulo;
    get_poly(poly1,poly2,n);

    flag = 0;
    M = mod0;
    multiply(poly1,poly2,result1,(1<<18));

    flag = 1;
    M = mod1;
    multiply(poly1,poly2,result2,(1<<18));

    M = modulo;
    mixing();



    fact[0]=1;
    for(int i=1;i<=100000;i++)    fact[i]=(fact[i-1]*(ll)i)%modulo;
    factor= 0;
    for(int i=0;i<A.size();i++){
        if ( i+1==A.size() ){
            factor++;
            constant_factor = (constant_factor*fact[factor])%modulo;
            break;
        }
        if ( A[i]!=A[i+1]){
            factor++;
            constant_factor = (constant_factor*fact[factor])%modulo;
            factor=0;
        }
        else    factor++;
    }
    constant_factor = powmod(constant_factor,M-2,M);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&x);
        index = upper_bound(A.begin(),A.end(),x) - A.begin();
        index--;
        ll answer = (fact[index]*((ll)B[index]*(ll)result[index])%M)%M;
        answer = (answer*constant_factor)%M;
        printf("%lld\n",answer);
    }
    return 0;
}
```