def s2(n, k):
    """Calculates the Stirling number of the second kind."""
    if n == 0 or n != 0 and n == k:
        return 1
    if k == 0 or n < k:
        return 0
    return k * s2(n-1, k) + s2(n-1, k-1)