Contiguous subsequences
Difficulty: Hard

Yash got a sequence of n positive integers ! He likes contiguous subsequences whose sum(mod)p = 0. He asks you to write a program to count them for him.

Input Format:
The first line contains T, the number of testcases.  
T testcases follow. Each testcase consists of 2 lines.   
The first line contains n and p separated by a single space.  
And the second line contains n space separated integers.

Output Format:
For each test case, output the number of contiguous subsequenences whose sum(mod)p = 0 in a newline.

Constraints   
1 ≤ T ≤ 20  
1 ≤ n ≤ 10^6   
1 ≤ k ≤ 100   
1 ≤ a[i] ≤ 10^4 

Sample Input:
2  
4 3  
1 2 3 4   
5 2   
1 2 1 2 1 

Sample Output:
4  
6

Python 3 starter code:


Reference solution (C++):
<pre><p> #include &lt;bits/stdc++.h> </p>
using namespace std;

int main(){  
    long long t;  
    cin>>t;  
    while(t--)  
    {  
        long long int n,k,i,j;  
        cin>>n>>k;  
        long long int x,sum[n+3],cnt[k+3];  
        sum[0]=0;  
        memset(cnt,0,sizeof(cnt));  
        cnt[0]=1;  
        for(i=1;i&lt;=n;i++)  
        {  
           cin>>x;  
           sum[i]=(sum[i-1]+x)%k;  
           cnt[sum[i]]++;  
        }  
        long long int res=0;  
        for(i=0;i&lt;k;i++)  
            res+=(cnt[i]*(cnt[i]-1))/2;  
        printf("%lld\n",res);  
        } 
        
    	return 0;
    }