#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
int t,n,e[5005][5005],out[5005];
int sum[5005],en[5005],cnt1[5005],cnt[5005],ans[5005][5005];
ll f[12500005] = {1},Ans;
char s[5005];
struct node
{
int out,id;
}a[5005];
bool cmp(node a,node b)
{
return a.out < b.out;
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin>>t;
for(int i = 1;i <= 12500000;i++)
f[i] = f[i - 1] * 2 % mod;
while(t--)
{
cin>>n;
for(int i = 1;i <= n;i++)
a[i].out = out[i] = 0,a[i].id = i;
for(int i = 2;i <= n;i++)
{
cin>>s;
for(int j = 1;j < i;j++)
{
e[i][j] = e[j][i] = 0;
int now;
if(s[(j - 1) / 4] >= 'A')
now = s[(j - 1) / 4] - 'A' + 10;
else now = s[(j - 1) / 4] - '0';
if(now & (1 << ((j - 1) % 4)))
e[i][j] = 1,a[i].out++,out[i]++;
else
e[j][i] = 1,a[j].out++,out[j]++;
}
}
sort(a + 1,a + n + 1,cmp);
for(int i = 1;i <= n;i++)
{
for(int j = a[i - 1].out;j < a[i].out;j++)
en[j] = i - 1;
sum[i] = sum[i - 1] + a[i].out;
if(sum[i] == i * (i - 1) / 2)
cnt[i] = cnt[i - 1] + 1;
else cnt[i] = cnt[i - 1];
if(sum[i] == i * (i - 1) / 2 + 1)
cnt1[i] = cnt1[i - 1];
else cnt1[i] = cnt1[i - 1];
}
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
{
if(!e[i][j])continue;
if(out[i] > out[j] + 1)
ans[j][i] = ans[i][j] = cnt[n] - (cnt[en[out[i] - 1]] - cnt[en[out[j]] - 1]);
else if(out[i] <= out[j])
ans[j][i] = ans[i][j] = cnt[n] + (cnt1[en[out[j]] - 1] - cnt1[en[out[i] - 1]]);
else ans[j][i] = ans[i][j] = cnt[n];
}
Ans = 0;
for(int i = 2;i <= n;i++)
for(int j = 1;j < i;j++)
(Ans += f[(i - 2) * (i - 1) / 2 + j - 1] * ans[i][j] % mod) % mod;
cout<<Ans<<endl;
}
}