#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 14, M = 1000000007;
int t, n, x, f[N][6], id;
ll ans;
map<int, int> st;
int Pow(int x, int n) {
int y = 1;
int u = x % M;
while (n) {
if (n & 1) y = (ll)y * u % M;
n >>= 1;
u = (ll)u * u % M;
}
return y;
}
int count(int x) {
return st[x];
}
void gen(int s, int e, int mul) {
if (s == e) {
if (id == 0)
++st[mul];
else ans += count(((ll)x * Pow(mul, M - 2)) % M);
}
else for (int i = 0; i < 6; ++i)
gen(s + 1, e, (ll)mul * f[s][i] % M);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i)
for (int j = 0; j < 6; ++j)
scanf("%d", &f[i][j]);
st.clear();
id = 0;
gen(0, n / 2, 1);
ans = 0; id = 1;
gen(n / 2, n, 1);
printf("%lld\n", ans);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTiA9IDE0LCBNID0gMTAwMDAwMDAwNzsKaW50IHQsIG4sIHgsIGZbTl1bNl0sIGlkOwpsbCBhbnM7Cm1hcDxpbnQsIGludD4gc3Q7CmludCBQb3coaW50IHgsIGludCBuKSB7CglpbnQgeSA9IDE7CglpbnQgdSA9IHggJSBNOwoJd2hpbGUgKG4pIHsKCQlpZiAobiAmIDEpIHkgPSAobGwpeSAqIHUgJSBNOwoJCW4gPj49IDE7CgkJdSA9IChsbCl1ICogdSAlIE07Cgl9CglyZXR1cm4geTsKfQoKaW50IGNvdW50KGludCB4KSB7CglyZXR1cm4gc3RbeF07Cn0KCnZvaWQgZ2VuKGludCBzLCBpbnQgZSwgaW50IG11bCkgewoJaWYgKHMgPT0gZSkgewoJCWlmIChpZCA9PSAwKQoJCQkrK3N0W211bF07CgkJZWxzZSBhbnMgKz0gY291bnQoKChsbCl4ICogUG93KG11bCwgTSA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