#include #include #include #include using mint = atcoder::modint998244353; mint solve_naive(int32_t n, int64_t m, std::vector a, std::vector w) { { std::vector a2; std::vector w2; for (int32_t i = 0; i < n; ++i) if (m % a[i] == 0) { a2.push_back(a[i]); w2.push_back(w[i]); } a.swap(a2); w.swap(w2); n = a.size(); } mint ans = 0; for (int32_t s = 1; s < 1 << std::min(n, 30); ++s) { mint prod = 1; int64_t l = 1; for (int32_t i = 0; i < n; ++i) if ((s >> i) & 1) { l = std::lcm(l, a[i]); prod *= w[i]; } if (l == m) { ans += prod; } } return ans; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int32_t t; int64_t m; std::cin >> t >> m; for (int32_t testcase = 0; testcase < t; ++testcase) { int32_t n; std::cin >> n; std::vector a(n); for (auto &e : a) std::cin >> e; std::vector w(n); for (auto &e : w) std::cin >> e; std::cout << solve_naive(n, m, a, w).val() << '\n'; } return 0; }