#include #include #include #include using mint = atcoder::modint998244353; std::vector prime_factors(int64_t m) { std::vector pf; for (int64_t p = 2; p * p <= m; ++p) { if (m % p == 0) { do m /= p; while (m % p == 0); pf.push_back(p); } } if (m != 1) { pf.push_back(m); } return pf; } mint solve(int32_t n, int64_t m, std::vector pf, std::vector a, std::vector w) { const int32_t k = pf.size(); std::vector zeta(1 << k, 1); for (int32_t i = 0; i < n; ++i) if (m % a[i] == 0) { int32_t t = 0; for (int32_t j = 0; j < k; ++j) { int64_t p = pf[j]; if ((m / p) % a[i] != 0) { t |= 1 << j; } } zeta[t] *= 1 + w[i]; } // Zeta transform for (int32_t block = 1; block < 1 << k; block *= 2) { for (int32_t offset = 0; offset < 1 << k; offset += 2 * block) { for (int32_t i = 0; i < block; ++i) { zeta[offset + block + i] *= zeta[offset + i]; } } } mint ans = 0; for (int32_t s = 0; s < 1 << k; ++s) { if (__builtin_popcount(s) & 1) { ans -= zeta[s]; } else { ans += zeta[s]; } } return ans - (m == 1); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int32_t t; int64_t m; std::cin >> t >> m; auto pf = prime_factors(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(n, m, pf, a, w).val() << '\n'; } return 0; }