Permutations and cycles (1)

Write a program to count the number of permutations of {1, …, n} with
exactly k cycles, where 1 ≤ k ≤ n.

For instance, of the six permutations of {1, 2, 3}, we have:

- two with one cycle, which are: (2, 3, 1) and (3, 1, 2).

- three with two cycles, which are: (2, 1, 3), (1, 3, 2) and (3, 2, 1).

- one with three cycles, which is: (1, 2, 3).

Input

Input consists of several cases, each with n and k, such that
1 ≤ k ≤ n ≤ 1000.

Output

For every case, count the number of permutations of {1, …, n} with k
cycles. As the result can be very large, make the computations modulo
10⁸ + 7.

Observation

Let c be the number of cases. The expected solution has total cost
O(1000² + c). You can get up to 80 points with test cases where n ≤ 100,
with a solution with cost O(100³ + c).

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:19:11.851Z

© Jutge.org, 2006–2026.
https://jutge.org
