human
Can you provide a code to product of first N factorials and the other steps required given the following:
Product of first N factorials | To compute ( a * b ) % MOD.
res = 0 Initialize result.
If b is odd , add ' a ' to result.
Multiply ' a ' with 2.
Divide b by 2.
Return result.
This function computes factorials and product by using above function i . e . modular multiplication.
Initialize product and fact with 1.
ith factorial.
product of first i factorials.
If at any iteration , product becomes divisible by MOD , simply return 0.
Driver Code to Test above functions"
How can the above be solved in Python?
assistant
#Here is code to product of first N factorials and the other steps required.
 def mulmod ( a , b , mod ) :
	a = a % mod
	while ( b > 0 ) :
		if ( b % 2 == 1 ) :
			res = ( res + a ) % mod
		a = ( a * 2 ) % mod
		b //= 2
	return res % mod
def findProduct ( N ) :
	product = 1 ; fact = 1
	MOD = 1e9 + 7
	for i in range ( 1 , N + 1 ) :
		fact = mulmod ( fact , i , MOD )
		product = mulmod ( product , fact , MOD )
		if not product :
			return 0
	return int ( product )
N = 3
print ( findProduct ( N ) )
N = 5
print ( findProduct ( N ) )