In the Mapping Department of Prime City, the Cartographer counts the number of onto functions from a set of m elements to a set of n elements. A surjection ensures every element in the target set is mapped to by at least one source element. "By inclusion-exclusion, the number of surjections from m to n is: sum of (-1)^k * C(n,k) * (n-k)^m for k = 0 to n," the Cartographer says. "This counts all functions, then subtracts those missing at least one target element." Given m and n, compute the number of surjections from a set of m elements to a set of n elements, modulo 10^9 + 7. Constraints: 0 <= m, n <= 5000 Input: 3 2 Output: 6 Input: 4 2 Output: 14
Constraints:
0 <= m, n <= 5000
Tags:
