Size three subsequences divisible by B

Given an array of integers A and an integer B.
Find and return the number of subsequences of length 3 whose sum is divisible by B.

Since the total number of subsequences may be very large.

Return the total number of subsequences of length 3 whose sum is divisible by B modulo (109+7).



Input Format

The only argument given is the integer array A.

Output Format

Return the number of subsequences of length 3 whose sum is divisible by B modulo (10^9+7). 

Constraints

1 <= length of the array <= 100000
1 <= A[i] <= 10^9 
1 <= B <= 10^3

For Example

Input 1:
    A = [6, 1, 1, 4, 1, 5, 3]
    B = 2
Output 1:
    20

Input 2:
    A = [4, 10, 9]
    B = 5
Output 2:
    0
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Size three subsequences divisible by B on Interview Code Editor
Sign Up
to access hints and editorial solutions for Size three subsequences divisible by B

Discussion


Loading...
Click here to start solving coding interview questions