def factorial_recur(n, mod=None):
if not (isinstance(n, int) and n >= <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if mod is not None and not (isinstance(mod, int) and mod > <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if n == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>result = n * factorial(n - <NUM_LIT:1>, mod)<EOL>if mod:<EOL><INDENT>result %= mod<EOL><DEDENT>return result<EOL>
Calculates factorial recursively.
    If mod is not None, then return (n! % mod)
    Time Complexity - O(n)