Binomial Modulo Small Prime — Solution & Editorial
Lucas' theorem: C(N, K) mod P = ∏ C(Ni, Ki) mod P over the base-P digits of N and K. Each small binomial uses factorials mod P.
Complexity: O(logP N · P)
Watch out: A digit of K exceeding the corresponding digit of N makes the whole product 0 — the core of Lucas.