Problem:
Balls and Boxes 6

Balls

Boxes

Any way

At most one ball

At least one ball

Distinguishable

Distinguishable

1

2

3

Indistinguishable

Distinguishable

4

5

6

Distinguishable

Indistinguishable

7

8

9

Indistinguishable

Indistinguishable

10

11

12

Problem

You have $n$ balls and $k$ boxes. You want to put these balls into the boxes.

Find the number of ways to put the balls under the following conditions:

Each ball is not distinguished from the other.

Each box is distinguished from the other.

Each ball can go into only one box and no one remains outside of the boxes.

Each box must contain at least one ball.

Note that you must print this count modulo $10^9+7$.

Input

$n$ $k$

The first line will contain two integers $n$ and $k$.

Output

Print the number of ways modulo $10^9+7$ in a line.

Constraints

$1 \le n \le 1000$

$1 \le k \le 1000$

Sample Input 1

5 3

Sample Output 1

6

Sample Input 2

10 5

Sample Output 2

126

Sample Input 3

100 30

Sample Output 3

253579538

Solution in Ruby (judge verdict: Accepted)

MOD = 1_000_000_007

FACT = [1]
(1..2000).each do |k|
  FACT << FACT[-1] * k % MOD
end

def exp(n, k)
  x = 1
  r = n
  while k > 0
    x *= r if k & 1 == 1
    r *= r
    x %= MOD
    r %= MOD
    k >>= 1
  end
  x
end

def inv(n)
  exp(n, MOD - 2)
end

def perm(n, k)
  n >= k ? FACT[n] * inv(FACT[n - k]) % MOD : 0
end

def comb(n, k)
  perm(n, k) * inv(FACT[k]) % MOD
end

n, k = gets.split.map(&:to_i)
p comb(n - 1, k - 1)