#include #include using namespace std; using namespace atcoder; using mint = modint998244353; std::vector fact, inv; void init(int n) { fact.resize(n + 1); inv.resize(n + 1); fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i; } inv[n] = fact[n].inv(); for (int i = n; i >= 1; i--) { inv[i - 1] = inv[i] * i; } } mint ncr(int n, int r) { if (r < 0 or n < r) return 0; return fact[n] * inv[r] * inv[n - r]; } int main() { init(600); int n, m; cin >> n >> m; vector a(n), b(n); for (auto &&x : a) { cin >> x; } for (auto &&x : b) { cin >> x; } int offset = 300; vector> count(m, vector(2 * offset + 1, 1)); for (int i = 0; i < n; i++) { for (int j = -offset; j <= offset; j++) { count.at(i % m).at(j + offset) *= ncr(a.at(i) + b.at(i), a.at(i) + j); } } int offset2 = (m + 1) / 2 * 300; vector ans(2 * offset2 + 1); ans.at(offset2) = 1; for (int i = 0; i < m; i++) { vector ans_new(2 * offset2 + 1); for (int j = 0; j <= 2 * offset2; j++) { for (int k = -offset; k <= offset; k++) { int j_next = j + k; if (j_next < 0 or 2 * offset2 < j_next) continue; ans_new.at(j_next) += ans.at(j) * count.at(i).at(k + offset); } } ans = move(ans_new); } cout << ans.at(offset2).val() << endl; return 0; }