// TLE #include #include #include #include using mint = atcoder::modint1000000007; using namespace std; // atcoder::static_modint

, P: prime number 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]; } }; mint solve_Onnm(int N, int M) { acl_fac fac(N + M); mint ret = 0; // 中央値がゼロ for (int nneg = 0; nneg <= (N - 1) / 2; nneg++) { for (int npos = 0; npos <= N / 2; npos++) { if (nneg == 0 and npos == 0) { if (M % 2 == 0) ret++; continue; } for (int m = M - npos - nneg; m >= 0; m -= 2) { ret += fac.ncr(N, nneg) * fac.ncr(N - nneg, npos) * fac.ncr(npos + nneg - 1 + m, m); } } } if (N % 2) { // 中央値が 1 または -1 for (int nneg = 0; nneg <= N / 2; nneg++) { for (int npos = 0; npos <= N / 2; npos++) { if (npos == 0 and nneg == 0) { if (M >= N and (M - N) % 2 == 0) ret++; continue; } const int n1 = N - nneg - npos; for (int m = M - npos * 2 - n1; m >= 0; m -= 2) { ret += fac.ncr(N, nneg) * fac.ncr(N - nneg, npos) * fac.ncr(npos + nneg - 1 + m, m) * 2; } // ダブルカウントを消す int rem = M - n1 - max(nneg, npos) * 2; for (int m = rem; m >= 0; m -= 2) { ret -= fac.ncr(N, nneg) * fac.ncr(N - nneg, npos) * fac.ncr(npos + nneg - 1 + m, m); } } } } return ret; } mint solve_Onm(int N, int M) { acl_fac fac(N + M); vector precalc(N + 1, vector(M + 1)); precalc[0][0] = 1; for (int n = 0; n <= N; n++) { for (int m = 0; m <= M; m++) { if (n + m - 1 >= 0) precalc[n][m] = fac.ncr(n + m - 1, n - 1); if (m >= 2) precalc[n][m] += precalc[n][m - 2]; } } mint ret = 0; if (N % 2 == 0) { int half = (N - 1) / 2; for (int nnonzero = 0; nnonzero <= half * 2; nnonzero++) { mint coeff = 0; for (int npos = 0; npos <= half; npos++) { int nneg = nnonzero - npos; if (nneg < 0 or nneg > half) continue; coeff += fac.ncr(N - 1, npos) * fac.ncr(N - 1 - npos, nneg); } mint tmp = 0; for (int b0 = 0; b0 <= M; b0++) { if (M - nnonzero - b0 < 0) continue; tmp += precalc.at(nnonzero).at(M - nnonzero - b0) * (b0 == 0 ? 1 : 2); } ret += tmp * coeff; } } else { for (int npos = 0; npos <= N / 2; npos++) { for (int nneg = 0; nneg <= N / 2; nneg++) { int nnonzero = npos + nneg; if (M - nnonzero < 0) continue; ret += fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][M - nnonzero]; } } for (int npos = 0; npos <= N / 2; npos++) { for (int nneg = 0; nneg <= N / 2; nneg++) { int nnonzero = npos + nneg; int m = M - npos - (N - nneg); if (m < 0) continue; ret += fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][m] * 2; } } for (int npos = 0; npos <= N / 2; npos++) { for (int nneg = 0; nneg <= N / 2; nneg++) { const int nnonzero = npos + nneg; int m = M - (N - nnonzero) - max(npos, nneg) * 2; if (m < 0) continue; ret -= fac.ncr(N, nnonzero) * fac.ncr(nnonzero, npos) * precalc[nnonzero][m]; } } } return ret; } int main() { for (int n = 2; n <= 50; n++) { for (int m = 0; m <= 50; m++) { if (solve_Onm(n, m) != solve_Onnm(n, m)) { cerr << n << ' ' << m << endl; throw; } } } int N, M; cin >> N >> M; cout << solve_Onnm(N, M).val() << '\n'; }