Easy
Calculate Factorial of a Number
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Modular Arithmetic
Description
Given a non-negative integer n, calculate and return its factorial modulo 1,000,000,007.
The factorial of a number is the product of all positive integers less than or equal to that number.
Mathematically:
n! = n * (n - 1) * (n - 2) * ... * 2 * 1
The factorial of zero is defined as 1. Therefore, 0! = 1 and 1! = 1.
Since factorial values grow very quickly, return the result modulo 1,000,000,007. This means returning the remainder after dividing n! by 1,000,000,007.
For example, 5! = 120, so the result for n = 5 is 120.
Implement the function factorial(n) that returns the factorial modulo 1,000,000,007.
Constraints
0 <= n <= 100000
Sample cases
3Sample 1
Input
n = 5
Expected output
120
Sample 2
Input
n = 0
Expected output
1
Sample 3
Skills and languages
Concepts
- Modular Arithmetic
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer