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;

typedef long long LL;

int main(){
LL test,n,k,x;
cin >> test;  
while(test --)  
{  
    cin >> n >> k; 
    LL sum[n+3],cnt[k+3];
    sum[0] = 0;  
    memset(cnt, 0, sizeof cnt);  
    cnt[0] = 1;  
    for(int i = 1; i &lt;= n; i ++)  
    {  
        cin >> x;  
        sum[i] = (sum[i-1] + x)%k;  
        cnt[ sum[i] ] ++;  
    }  
  
        LL res = 0;  
        for(int r = 0; r < k; r ++)  
            res += (cnt[r]*(cnt[r]-1)/2);  
        cout << res << endl;  
    } 
    }