Factorial Modulo - aloalgo

Factorial Modulo

Easy

You are given a non-negative integer n.

Your task is to compute n! (n factorial) modulo 10^9 + 7 (1000000007).

Remember that:

  • n! (n factorial) is the product of all positive integers from 1 to n.
  • By convention, 0! = 1.
  • 10^9 + 7 is a large prime number commonly used as a modulus to prevent integer overflow.
  • You should take the modulo at each step of the computation to avoid overflow.

Return n! % (10^9 + 7).

Example 1

Input
5
Output
120
Explanation:

5! = 120. Since 120 < 10^9 + 7, the result is simply 120.

Example 2

Input
13
Output
227020758
Explanation:

13! = 6,227,020,800. Since this exceeds 10^9 + 7, we return 6,227,020,800 % 1,000,000,007 = 227,020,758.

Example 3

Input
0
Output
1
Explanation:

0! = 1 by convention. Since 1 < 10^9 + 7, the result is 1.

Loading...
Input
5
Output
120

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!