def is_fermat_probable_prime(n, base=2):
    """is_fermat_probable_prime(n [, base]) -> 0|1|2

    Return a three-state flag (either 0, 1 or 2) that integer ``n `` is
    either a prime or Fermat pseudoprime, as witnessed by one or more
    integer bases.

    Arguments
    ---------

        n       Integer to be tested for primality.
        base    Optional integer base, or tuple of bases. (Defaults to 2.)

    Return result
    -------------

        0       Number is definitely non-prime.
        1       Number is definitely prime.
        2       Number is a weak probable prime or pseudoprime.


    ``is_fermat_probable_prime`` performs the Fermat primality test,
    which is a weak probabilistic test. If a number fails this test,
    it is definitely composite (there are no false negative tests):

    >>> is_fermat_probable_prime(99, 7)
    0

    However, if the number passes the test, it is provisional evidence
    that it may be a prime:

    >>> is_fermat_probable_prime(29, 7)  # 29 actually is prime.
    2

    In this case we can state that "7 is a witness that 29 may be prime".

    As the Fermat test is probabilistic, composite numbers will sometimes
    pass a test, or even repeated tests. We call them pseudoprimes to
    some base:

    >>> is_fermat_probable_prime(3*11, 10)  # 33 is a pseudoprime to base 10.
    2

    and we call 10 a "Fermat liar" for 33.

    A single passed test is not very convincing, but with more tests, we
    can gain more confidence. ``base`` must be a positive int between 1
    and n-1 inclusive, or a tuple of such bases. 1 is permitted, but not
    very useful: it is a witness for all numbers. By default, base=2.

    >>> is_fermat_probable_prime(33, (10, 7))
    0

    It may take an arbitrary number of Fermat tests to definitively
    prove a number is composite:

    >>> is_fermat_probable_prime(7*11*13*41, (17, 23, 356, 359))
    2
    >>> is_fermat_probable_prime(7*11*13*41, (17, 23, 356, 359, 363))
    0

    Unfortunately, there are some numbers which are composite but still
    pass *all* Fermat tests. These pseudoprime numbers are called the
    Carmichael numbers, and ``is_fermat_probable_prime`` cannot
    distinguish them from actual primes no matter how many tests you
    perform.

    For large enough ``n``, if a number passes ``k`` randomly chosen and
    independent Fermat tests, we can conclude that the probability that
    it is either prime or a Carmichael number is (on average) at least
    ``1 - (1/2**k)``.
    """
    if not isinstance(base, tuple):
        base = (base,)
    # Deal with the simple deterministic cases first.
    if n < 2:
        return 0  # Certainly composite (or unity, or zero).
    elif n == 2:
        return 1  # Certainly prime.
    elif n % 2 == 0:
        return 0
    # Now the Fermat test proper.
    for a in base:
        if pow(a, n-1, n) != 1:
            return 0  # n is certainly composite.
    return 2