"""Binomial coefficients: Pascal's triangle and n choose k mod a prime.

Idea
----
C(n, k) counts the ways to choose k items from n. Pascal's rule
C(n, k) = C(n-1, k-1) + C(n-1, k) builds a triangle row by row with only
additions. For a single value with a large n, use the factorial formula
n! / (k! (n-k)!) modulo a prime p, dividing via the modular inverse
x^(p-2) mod p (Fermat's little theorem).

Python's ``math.comb`` computes exact values directly; these versions show
the two techniques you need when a problem asks for the answer modulo
10^9 + 7.

Complexity
----------
pascal_row(n):        O(n^2) time to build, O(n) space per row
comb_mod(n, k, p):    O(n + log p) time, O(1) extra space
"""

MOD = 1_000_000_007


def pascal_row(n: int) -> list[int]:
    """Return row n of Pascal's triangle: [C(n, 0), ..., C(n, n)]."""
    row = [1]
    for _ in range(n):
        row = [1] + [a + b for a, b in zip(row, row[1:])] + [1]
    return row


def comb_mod(n: int, k: int, p: int = MOD) -> int:
    """C(n, k) mod a prime p (requires n < p)."""
    if k < 0 or k > n:
        return 0
    k = min(k, n - k)
    numerator = denominator = 1
    for i in range(k):
        numerator = numerator * (n - i) % p
        denominator = denominator * (i + 1) % p
    return numerator * pow(denominator, p - 2, p) % p
