code: def factorial(n, mod=None):
      
      if not (isinstance(n, int) and n >= 0):
          raise ValueError("'n' must be a non-negative integer.")
      if mod is not None and not (isinstance(mod, int) and mod > 0):
          raise ValueError("'mod' must be a positive integer")
      result = 1
      if n == 0:
          return 1
      for i in range(2, n+1):
          result *= i
          if mod:
              result %= mod
      return result
docs: Calculates factorial iteratively.
      If mod is not None, then return (n! % mod)
      Time Complexity - O(n)