def binomial( n, k ):
    """Calculate the binomial coefficient n over k."""
    if k < 0:
        return 0
    if n < 0:
        return binomial( -n + k - 1, k ) * (-1)**k

    b = 1
    for i in range(k):
        b *= (n-i)
        b /= (1+i)
    return b