#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e9 + 7;
int n, x, F[14][6], all[280000], idx;
ll res;
int Pow(int a, int b) {
int res = 1;
while (b != 0) {
if (b & 1)
res = ((ll) res * a) % M;
a = ((ll) a * a) % M;
b >>= 1;
}
return res;
}
inline int calc(int cur) {
int L = lower_bound(all, all + idx, cur) - all;
if (L == idx)
return 0;
return upper_bound(all, all + idx, cur) - all - L;
}
void calc(int at, int cur, int E) {
if (at == E) {
if (E == n)
res += calc(((ll) x * Pow(cur, M - 2)) % M);
else
all[idx++] = cur;
return;
}
for (int i = 0; i < 6; ++i)
calc(at + 1, ((ll) cur * F[at][i]) % M, E);
}
int main(int argc, char **argv) {
int t;
scanf("%d", &t);
while (t-- != 0) {
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i)
for (int j = 0; j < 6; ++j)
scanf("%d", &F[i][j]);
res = idx = 0;
calc(0, 1, n >> 1);
sort(all, all + idx);
calc(n >> 1, 1, n);
printf("%lld\n", res);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCmNvbnN0IGludCBNID0gMWU5ICsgNzsKaW50IG4sIHgsIEZbMTRdWzZdLCBhbGxbMjgwMDAwXSwgaWR4OwpsbCByZXM7CgppbnQgUG93KGludCBhLCBpbnQgYikgewoJaW50IHJlcyA9IDE7Cgl3aGlsZSAoYiAhPSAwKSB7CgkJaWYgKGIgJiAxKQoJCQlyZXMgPSAoKGxsKSByZXMgKiBhKSAlIE07CgkJYSA9ICgobGwpIGEgKiBhKSAlIE07CgkJYiA+Pj0gMTsKCX0KCXJldHVybiByZXM7Cn0KCmlubGluZSBpbnQgY2FsYyhpbnQgY3VyKSB7CglpbnQgTCA9IGxvd2VyX2JvdW5kKGFsbCwgYWxsICsgaWR4LCBjdXIpIC0gYWxsOwoJaWYgKEwgPT0gaWR4KQoJCX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