// 3乗解で, N <= 100 の全ケースと,given N <= 400 のときその N で想定解チェック #include #include #include #include using namespace std; template struct acl_fac { std::vector facs, facinvs; acl_fac(int N) { assert(-1 <= N and N < modint::mod()); facs.resize(N + 1, 1); for (int i = 1; i <= N; i++) facs[i] = facs[i - 1] * i; facinvs.assign(N + 1, facs.back().inv()); for (int i = N; i > 0; i--) facinvs[i - 1] = facinvs[i] * i; } modint ncr(int n, int r) const { if (n < 0 or r < 0 or n < r) return 0; return facs[n] * facinvs[r] * facinvs[n - r]; } modint operator[](int i) const { return facs[i]; } modint finv(int i) const { return facinvs[i]; } }; using mint = atcoder::modint1000000007; acl_fac fac(1000000); mint solve(int N, int M) { acl_fac fac(N); vector ncrs(N + 1); for (int i = 0; i < int(ncrs.size()); i++) ncrs[i] = fac.ncr(N, i); vector ncr_cums(ncrs.size() + 1); for (int i = 0; i < int(ncrs.size()); i++) ncr_cums[i + 1] = ncr_cums[i] + ncrs[i]; mint ret = 0; auto add_closed = [&](int l, int r) { // if (l > r) return; assert(l <= r + 1); l = max(l, 0); r = min(r, N); ret += ncr_cums[r + 1] - ncr_cums[l]; }; for (int h = 1; h <= N; h++) { if (M - (N - M) >= h) { add_closed(M, N); } else { int l = h + (N - h + 1) / 2; add_closed(l, N); if ((N - h) % 2) { add_closed(l, l - 1 + l - M); } else { add_closed(l + 1, l + l - M); } } } const int K = N - M; for (int h = 1; h <= K; h++) { const int l = h + (N - h + 1) / 2; if (l <= K) { add_closed(l, K); if ((N - h) % 2) { add_closed(l, N); } else { add_closed(l + 1, N); } } else { add_closed(h + N - K, N); } } for (int i = M; i <= N; i++) ret += fac.ncr(N, i) * N; return ret; } void validate_n3(int N) { vector dp(N + 1, vector(N + 1)); // dp[i][j] = 今終わったら ')' が i 個必要でここまで '(' を j 個使ったような場合の数 vector sum(N + 1, vector(N + 1)); dp[0][0] = 1; for (int t = 0; t < N; t++) { vector dpnxt(dp.size(), vector(dp[0].size())); vector sumnxt(dp.size(), vector(sum[0].size())); for (int i = 0; i < int(dp.size()); i++) { for (int j = 0; j < int(dp[i].size()); j++) { if (dp[i][j] == 0 and sum[i][j] == 0) continue; // ')' を使う場合 dpnxt[max(0, i - 1)][j] += dp[i][j]; sumnxt[max(0, i - 1)][j] += sum[i][j]; if (!i) sumnxt[i][j] += dp[i][j]; // '(' を使う場合 dpnxt[i + 1][j + 1] += dp[i][j]; sumnxt[i + 1][j + 1] += sum[i][j]; } } dp = dpnxt; sum = sumnxt; } vector ret(N + 1); for (int i = 0; i < int(dp.size()); i++) { for (int j = 0; j < int(dp[i].size()); j++) { ret[j] += sum[i][j] + dp[i][j] * i; } } for (int i = 0; i <= N; i++) ret[i] += fac.ncr(N, i) * N; for (int i = int(ret.size()) - 1; i; i--) ret[i - 1] += ret[i]; for (int m = 0; m <= N; m++) assert(solve(N, m) == ret[m]); } int main() { for (int n = 1; n <= 50; n++) validate_n3(n); int N, M; cin >> N >> M; if (N <= 400) validate_n3(N); cout << solve(N, M).val() << '\n'; }