using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 3");
//36
}
else if (InputPattern == "Input2") {
WillReturn.Add("10 3");
//55980
}
else if (InputPattern == "Input3") {
WillReturn.Add("100 100");
//437918130
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
const long Hou = 1000000007;
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long N = wkArr[0];
long K = wkArr[1];
// 包除原理で解く
long Answer = DerivePatternCnt(N, K);
// 0個にする箱の数でループ
for (long I = 1; I <= K - 1; I++) {
long PatternCnt = DerivePatternCnt(N, K - I);
PatternCnt %= Hou;
PatternCnt *= DeriveChoose(K, I);
PatternCnt %= Hou;
if (I % 2 == 1) {
Answer -= PatternCnt;
}
else {
Answer += PatternCnt;
}
Answer %= Hou;
if (Answer < 0) Answer += Hou;
}
Console.WriteLine(Answer);
}
// ボールの数と箱の数を引数として、配置の場合の数を返す
static long DerivePatternCnt(long pN, long pK)
{
long WillReturn = 1;
for (long I = 1; I <= pN; I++) {
WillReturn *= pK;
WillReturn %= Hou;
}
return WillReturn;
}
// nCr (mod Hou)を求める
static long DeriveChoose(long pN, long pR)
{
pR = Math.Min(pR, pN - pR);
long WillReturn = 1;
for (long I = pN - pR + 1; I <= pN; I++) {
WillReturn *= I;
WillReturn %= Hou;
}
for (long I = 2; I <= pR; I++) {
WillReturn *= DeriveGyakugen(I);
WillReturn %= Hou;
}
return WillReturn;
}
// 引数の逆元を求める
static long DeriveGyakugen(long pLong)
{
return DeriveBekijyou(pLong, Hou - 2, Hou);
}
// 繰り返し2乗法で、(NのP乗) Mod Mを求める
static long DeriveBekijyou(long pN, long pP, long pM)
{
long CurrJyousuu = pN % pM;
long CurrShisuu = 1;
long WillReturn = 1;
while (true) {
// 対象ビットが立っている場合
if ((pP & CurrShisuu) > 0) {
WillReturn = (WillReturn * CurrJyousuu) % pM;
}
CurrShisuu *= 2;
if (CurrShisuu > pP) return WillReturn;
CurrJyousuu = (CurrJyousuu * CurrJyousuu) % pM;
}
}
}