#include #include using namespace std; using namespace atcoder; using mint = modint998244353; int main() { 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) *= max(-1, min({a.at(i), b.at(i), j + a.at(i), b.at(i) - j})) + 1; } } 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; }