def factorial(num):
    """Returns the factorial of a nonnegative integer.
    
    This function is provided in math module starting with Python 2.6,
    but implement anyway for compatibility with older systems."""
    result = 1
    for i in range(2, num + 1):
        result = result * i
    return result
