Problem:
Balls and Boxes 3

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 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

4 3

Sample Output 1

36

Sample Input 2

10 3

Sample Output 2

55980

Sample Input 3

100 100

Sample Output 3

437918130

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
const int MAX = 510000;
const ll mod = 1e9+7;

ll fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++){
    fac[i] = fac[i-1] * (ll)i % mod;
    inv[i] = mod - inv[mod%i] * (mod/(ll)i) % mod;
    finv[i] = finv[i-1] * inv[i] % mod;
  }
}

ll COM(int n, int k){
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

ll Pow(ll x, ll n){
  if(n == 0) return 1;
  if(n % 2 == 0) return Pow(x*x % mod,n/2);
  else return x * Pow(x,n-1) % mod;
}

int main(){
  ll n, k;
  cin >> n >> k;
  COMinit();
  if(n < k) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  rep(i,k) {
    if(i % 2 == 0) (ans += Pow(k-i,n) * COM(k,k-i)) %= mod;
    else (ans -= Pow(k-i,n) * COM(k,k-i)) %= mod;
  }
  if(ans < 0) ans += mod;
  cout << ans << endl;
  return 0;
}