factorial MOD 998244353
Here is an example of how you can calculate the factorial of a number in C++ and take the modulo 998244353 at each step:
#include <iostream>
int factorialMod(int n) {
const int MOD = 998244353;
int result = 1;
for (int i = 1; i <= n; i++) {
result = (result * i) % MOD;
}
return result;
}
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
int factorial = factorialMod(n);
std::cout << "Factorial Mod 998244353 of " << n << " is: " << factorial << std::endl;
return 0;
}
Explanation for each step:
We include the necessary header file
iostream
to use input/output stream functions in C++.We define a function
factorialMod
that takes an integern
as input and returns the factorial modulo 998244353. We initialize a variableresult
to 1 to store the factorial result.We define a constant integer
MOD
and set its value to 998244353. This value will be used for taking the modulo at each step.We use a
for
loop to iterate from 1 ton
. In each iteration, we multiplyresult
withi
and take the moduloMOD
to avoid overflow.Finally, we return the calculated
result
as the factorial modulo 998244353.In the
main
function, we prompt the user to enter a number and store it in the variablen
.We call the
factorialMod
function with the input numbern
and store the result in the variablefactorial
.We then output the factorial modulo 998244353 of the input number
n
using thestd::cout
function.Finally, we return 0 to indicate successful execution of the program.
Note: The modulo operation is performed at each step to prevent the result from becoming too large and causing an overflow. The constant MOD
is chosen as 998244353, which is a prime number commonly used in competitive programming to avoid overflow issues.