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:

  1. We include the necessary header file iostream to use input/output stream functions in C++.

  2. We define a function factorialMod that takes an integer n as input and returns the factorial modulo 998244353. We initialize a variable result to 1 to store the factorial result.

  3. We define a constant integer MOD and set its value to 998244353. This value will be used for taking the modulo at each step.

  4. We use a for loop to iterate from 1 to n. In each iteration, we multiply result with i and take the modulo MOD to avoid overflow.

  5. Finally, we return the calculated result as the factorial modulo 998244353.

  6. In the main function, we prompt the user to enter a number and store it in the variable n.

  7. We call the factorialMod function with the input number n and store the result in the variable factorial.

  8. We then output the factorial modulo 998244353 of the input number n using the std::cout function.

  9. 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.