Given an array of positive integers, count the number of distinct pairs of indices $$$ (i, j) $$$ such that the product of the elements at those indices has a residue of $$$ K $$$ modulo $$$ M $$$. That is, find the number of pairs $$$ (i, j) $$$ such that:
$$$\text{arr}[i] \times \text{arr}[j] \mod M = K$$$
It's made up problem so assume all values are upto $$$ 10^5 $$$
The $$$ K = 0 $$$ case is simplest one as it involves counting multiples to cancel out denominator, but I am not able to think anything further, I do have feeling that there must be a general way since $$$ K = 0 $$$ case is solvable.
$$$M$$$ is also not prime otherwise it would be solvable easily.
Originally I was solving the following, count pairs in array where this value,
$$$\text{arr}[i] \times \text{arr}[j] + \text{arr}[i] + \text{arr}[j]$$$
is divisible by $$$M$$$.
Since, $$$(x \cdot y + x + y) = (x + 1)(y + 1) - 1$$$ it's same as original problem with $$$K = 1$$$ and new array being $$$arr'[i] = arr[i] + 1$$$.
Would like to know if someone has a solution.