import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum MO = 10L^^9 + 7; int N, K; void main() { try { for (; ; ) { N = readInt(); K = readInt(); auto dp2 = new long[][](K + 2, K + 2); dp2[0][1] = 1; foreach (k; 0 .. K) { auto dp2Next = new long[][](K + 2, K + 2); foreach (x; 0 .. K + 2) foreach (y; 0 .. K + 2) { long lot = N; if (x == y) { if ((K + 1) - x > 0) { --lot; (dp2Next[x + 1][y + 1] += dp2[x][y]) %= MO; } } else { if ((K + 1) - x > 0) { --lot; (dp2Next[x + 1][y] += dp2[x][y]) %= MO; } if ((K + 1) - y > 0) { --lot; (dp2Next[x][y + 1] += dp2[x][y]) %= MO; } } (dp2Next[x][y] += dp2[x][y] * lot) %= MO; } dp2 = dp2Next; } debug { writeln("dp2 = ", dp2); } auto sum2 = new long[K + 2]; foreach (x; 0 .. K + 2) foreach (y; 0 .. K + 2) { if (x < y) { (sum2[x] += dp2[x][y]) %= MO; } } long ans; foreach (i; 1 .. N + 1) { if (i > K + 1) { foreach (x; 0 .. K + 2) { ans += sum2[x] * (i - x); ans %= MO; } } else { auto dp1 = new long[K + 2]; dp1[0] = 1; foreach (k; 0 .. K) { auto dp1Next = new long[K + 2]; foreach (x; 0 .. K + 2) { long lot = N; if (i - x > 0) { --lot; (dp1Next[x + 1] += dp1[x]) %= MO; } (dp1Next[x] += dp1[x] * lot) %= MO; } dp1 = dp1Next; } debug { writeln("i = ", i, ": dp1 = ", dp1); } foreach (x; 0 .. i) { ans += (dp1[x] - dp2[x][x]) * (i - x); ans %= MO; } } } ans = (ans % MO + MO) % MO; writeln(ans); } } catch (EOFException e) { } }