I wrote the following program in python for the following codechef question http://www.codechef.com/problems/MOVES/
import sys
tokenizedInput = sys.stdin.read().split()
mod=1000000007
arr=[1]*5001
for i in range(1,5001):
arr[i]=(arr[i-1]*i)%mod
def combo(r,n,mod):
q=arr[n]
print q
r=(arr[r]*arr[n-r])
print r
return ((q/r)%mod)
elm=0
for i in range (0,5001):
n=int(tokenizedInput[elm])
elm=elm+1
k=int(tokenizedInput[elm])
elm=elm+1
if(n==0 and k==0):
break
out=0
if(((k-1)/2)!=(k/2)):
out=(2*combo((k-1)/2,n-2,mod)*combo(k/2,n-2,mod))%mod
else:
out=(2*combo(k/2,n-2,mod)**2)%mod
print out
but my modulo function is not working correctly for example for values n=498 and r=2 the answer returned by combo() is 0 because q=243293343 and r=1428355228 how to perform my modulo operation in arr[] to rectify this error ?