I was calculating (n!/m!) mod 10^9 (not 10^9+7) . So I used following code to pre-calculate all factorials as:

fact[0]=1;

for(i=1;i<=n;i++)

{

fact[i] = (fact[i-1]*i)%mod; //**mod = 10^9**

}

1<=m<=n<=1000

But somewhere in between it introduced 0 and that 0 propagates till n . So how to overcome such situation ?**And yes it is working fine if mod = 10^9+7** .

Sorry for bad language .

In above code I am storing factorial of any n<=1000. And final result will be calculated directly by

ans = (fact[n]/fact[m]) % mod .

But my fact array working fine till fact[39] .

i.e

fact[1]=1

fact[2]=2

fact[3]=6

fact[4]=24

fact[5]=120

fact[6]=720

.

.

fact[39]=800000000

fact[40]=0 //because (fact[39]*40) % 10^9 = 0

and therefore fact[41]=fact[42]= … fact[1000]=0 .

So how to overcome such situation ?