Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For calculating the value of choosing $r$ items from $n$ items where $q$ are of same kind, and we should take modulo $m$, where $m$ is a prime; I used the following relation:

$$\frac{\,^{n}P_{r}}{q!} \pmod{m}$$

For calculating this

  • I first calculated $n!$, then $n!\bmod{m}$

  • Next, I calculated $(n-r)!$ and multiplied it with $q!$, i.e $t = q!(n-r)!$

  • Then I multiplied the modular multiplicative inverse of $t$ with $n!$ and took the result modulo $m$ (i.e. $t^{-1}n!\bmod{m}$)

But I am not getting the correct answer.. E.g: if $n= 3$, $r = 2$, $q = 2$ then the expected result is $\frac{\,^{3}P_{2}}{2!}\equiv3\pmod{1000000007}$ but am getting $250000004$.. I can't understand my mistake here.. Thanks.

share|cite|improve this question

It's difficult to see what's going wrong here. How are you computing the multiplicative inverse of 2? Regardless of that, what value do you get for $2^{-1} \pmod{1000000007}$ ? (Since the modulus is odd, the answer should be immediately obvious.)

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.