product of all the integers less than or equal to n having same remainders when divided by m and are also equal to n%m

120 Views Asked by At

Given two integers n and m, what is the product of all the integers <= n having same remainders when divided by the number m and the remainders are equal to n%m? As the numbers can be too large find the product % (10^9+7)

Here $0<=n<=2*10^9$, $1<=m<=2*10^9$ and $n/m<=10^6$

As the constraints are too large I need a simple way to find the answer

Example: For n=8 and m=3, there are three numbers: 2,5 and 8<=n which give same remainders when divided my m=3 and the remainders are also equal to n%m , i.e,2 so the answer is $2*8*5=80$. Now the final answer is 80 modulus (10^9+7) which equals 80

So I need a simpler way to evaluate the answer for larger values of n and m