Problem:
Balls and Boxes 1

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 can contain an arbitrary number of balls (including zero).

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

2 3

Sample Output 1

9

Sample Input 2

10 5

Sample Output 2

9765625

Sample Input 3

100 100

Sample Output 3

424090053

Solution in Scala (judge verdict: Accepted)

import java.util.Scanner

object Main extends App { 
  val sc = new Scanner(System.in)
  val N, K = sc.nextInt()
  val Base = 1000000007

  def expMod(base:Long, exponent:Long, mod:Long):Long = {
    require(base >= 0,     "Base must not be negative. Base="+base)
    require(exponent >= 0, "Exponent must not be negative. Exponent="+exponent)
    require(mod > 1, "Modulo must be grater than 1. Modulo="+mod)
    def iter(b:Long, e:Long, p:Long):Long = {
      (e, e & 1) match {
        case (0, _) => p
        case (_, 0) => iter((b*b) % mod, e >> 1, p % mod)
        case (_, _) => iter((b*b) % mod, e >> 1, (b*p) % mod)
      }
    }

    iter(base, exponent, 1)
  }

  println(expMod(K,N,Base))

}