// #1

/*
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
typedef long long ll;
using namespace std;

int n;
int nums[200010];
vector<int> pos[200010];
int l[200010];
int r[200010]; //l and r store first occurence of the number to its left and right
int bit[200010];
ll sum(int idx)
{
    idx ++;
    ll ans = 0;
    while(idx > 0)
    {
        ans += bit[idx];
        idx -= (idx & (-idx));
    }
    return ans;
}
void update(int idx, ll val)
{
    if(idx == -1) return;
    idx ++;
    while(idx < n + 1)
    {
        bit[idx] += val;
        idx += (idx & (-idx));
    }
}
int main()
{
    cin >> n;
    ll ans = 0;
    for(int i = 0; i < n; i ++)
    {
        cin >> nums[i]; nums[i] --;
        pos[nums[i]].push_back(i);
    }
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < pos[i].size(); j ++)
        {
            if(j == 0) l[pos[i][j]] = -1;
            else l[pos[i][j]] = pos[i][j - 1];
            if(j == pos[i].size() - 1) r[pos[i][j]] = n;
            else r[pos[i][j]] = pos[i][j + 1];
        }
    for(int i = 1; i < n; i ++)
    {
        ans += (i - (l[i] + 1) - (sum(i-1) - sum(l[i])));
        update(l[i], 1);
    }
    cout << ans << "\n";
}
*/


// #2
/*
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <unordered_set>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define f first
#define s second

struct dsu
{
    int sz[200010];
    int par[200010];
    dsu(int n)
    {
        for(int i = 0; i <= n; i ++)
        {
            sz[i] = 1;
            par[i] = i;
        }
    }
    int root(int cur)
    {
        if(par[cur] == cur) return cur;
        return par[cur] = root(par[cur]);
    }
    bool same(int n, int m)
    {
        if(root(n) == root(m)) return true;
        return false;
    }
    void un(int n, int m)
    {
        n = root(n);
        m = root(m);
        if(same(n, m)) return;
        if(sz[n] < sz[m]) swap(n, m);
        par[m] = n;
        sz[n] += sz[m];
    }
};
int n, ans = 0, mn, sz1, sz2, idx, fil;
int node[100010][4];
vector<int> portals[200010];
int cost[100010];
pii sorted[100010];
vector<int> adj[200010];
int visited[200010];
int other[200010];
void dfs(int cur, dsu* d)
{
    if(visited[cur] == fil) return;
    visited[cur] = fil;
    if(d->same(other[cur], idx)) mn = min(mn, cost[cur/2]);
    for(auto it : adj[cur])
    {
        if(visited[it] == fil) continue;
        dfs(it, d);
    }
}
int main()
{
    cin >> n;
    dsu d = dsu(n*2);
    for(int i = 0; i < n; i ++)
    {
        cin >> cost[i];
        sorted[i].f = cost[i]; sorted[i].s = i;
        for(int j = 0; j < 4; j ++)
        {
            cin >> node[i][j]; node[i][j] --;
            portals[node[i][j]].push_back(i*2 + j/2);
        }
    }
    sort(sorted, sorted + n);
    for(int i = 0; i < 2 * n; i ++)
    {
        d.un(portals[i][0], portals[i][1]);
        adj[portals[i][0]].push_back(portals[i][1]); adj[portals[i][1]].push_back(portals[i][0]);
        other[i] = ((i%2==0)?(i+1):(i-1));
    }
    int i;
    for(int j = 0; j < n; j ++)
    {
        i = sorted[j].s;
        if(d.same(i*2, i*2 + 1)) continue;
        sz1 = d.sz[d.root(i*2)]; sz2 = d.sz[d.root(i*2 + 1)];
        mn = 2000000000; fil = i+1;
        if(sz1 < sz2)
        {
            idx = i*2 + 1;
            dfs(i*2, &d);
        }
        else
        {
            idx = i*2;
            dfs(i*2 + 1, &d);
        }
        ans += mn;
        d.un(i*2, i*2 + 1);
        adj[i*2].push_back(i*2 + 1); adj[i*2 + 1].push_back(i*2);
    }
    cout << ans << "\n";
    return 0;
}
*/

// #3

/*
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define f first
#define s second

ll n, ans = 0, cnt;
bool good, good2;
ll mod = 1000000007;
pll nums[50];
ll fac[50];
vector<int> cur;
vector<pii> con;
bool used[50];
ll crossProd(pll p1, pll p2)
{
    return p1.f * p2.s - p1.s * p2.f;
}
int ccw(pll p1, pll p2, pll p3)
{
    ll tmp = crossProd({p2.f - p1.f, p2.s - p1.s}, {p3.f - p1.f, p3.s - p1.s});
    if(tmp > 0) return 1;
    return -1;
}
int seg(pll p1, pll p2, pll p3, pll p4)
{
    if(ccw(p1, p2, p3) != ccw(p1, p2, p4) && ccw(p3, p4, p1) != ccw(p3, p4, p2)) return 1;
    return 0;
}
void perm(int idx)
{
    if(idx == n)
    {
        good = true;
        con.clear();
        con.push_back({cur[0], cur[1]}); con.push_back({cur[1], cur[2]}); con.push_back({cur[2], cur[0]});
        for(int i = 3; i < n; i ++)
        {
            cnt = 0;
            for(int j = 0; j < i; j ++)
            {
                good2 = true;
                for(int k = 0; k < con.size(); k ++)
                {
                    if(con[k].f == cur[j] || con[k].f == cur[i] || con[k].s == cur[j] || con[k].s == cur[i]) continue;
                    if(seg(nums[cur[i]], nums[cur[j]], nums[con[k].f], nums[con[k].s]))
                    {
                        good2 = false;
                        break;
                    }
                }
                if(good2) { con.push_back({cur[i], cur[j]}); cnt ++; }
            }
            if(cnt != 3) { good = false; break; }
        }
        if(good) ans ++;
        return;
    }
    for(int i = 0; i < n; i ++)
    {
        if(used[i]) continue;
        cur.push_back(i);
        used[i] = true;
        perm(idx + 1);
        cur.pop_back();
        used[i] = false;
    }
}
int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++)
        cin >> nums[i].f >> nums[i].s;
    fac[0] = 1;
    for(int i = 1; i < n; i ++)
        fac[i] = (fac[i-1] * i) % mod;
    if(n < 9)
    {
        perm(0);
        cout << ans << "\n";
    }
    else
    {
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < n; j ++)
                for(int k = 0; k < n; k ++)
                {
                    if(i == j || i == k || j == k) continue;
                    cnt = 0;
                    for(int l = 0; l < n; l ++)
                    {
                        if(l == i || l == j || l == k) continue;
                        if(seg(nums[l], nums[i], nums[j], nums[k]) || seg(nums[l], nums[j], nums[i], nums[k]) || seg(nums[l], nums[k], nums[j], nums[i]))
                            cnt ++;
                    }
                    ans = (ans + fac[n-3-cnt] * fac[cnt]) % mod;
                }
        cout << ans << "\n";
    }
}
*/




























#include <iostream>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <iomanip>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<int, char> pic;
typedef pair<pii, int> piii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
//#define max(n, m) ((n>m)?n:m)
//#define min(n, m) ((n<m)?n:m)
#define f first
#define s second
/*
unordered_set<int> adj[200010], rev[200010], adj2[200010];
bool visited[200010];
int scc[200010];
vector<int> sc[200010];
queue<int> q;
int in[200010];
stack<int> ord;
int curCC = 1;
void dfs1(int cur)
{
    visited[cur] = true;
    for(auto it : adj[cur])
    {
        if(visited[it]) continue;
        dfs1(it);
    }
    ord.push(cur);
}
void dfs2(int cur)
{
    scc[cur] = curCC;
    sc[curCC].push_back(cur);
    for(auto it : rev[cur])
    {
        if(scc[it]) continue;
        dfs2(it);
    }
}
int other[200010];
char ans[100010];
int main()
{
    char ch1, ch2;
    int n, m, a, b, c1, c2, cur;
    cin >> n >> m;
    for(int i = 0; i < m*2; i ++)
        other[i] = ((i%2==0)?i+1:i-1);
    for(int i = 0; i < n; i ++)
    {
        cin >> ch1 >> a >> ch2 >> b; a--; b--;
        if(ch1 == '+') c1 = 0; else c1 = 1;
        if(ch2 == '+') c2 = 0; else c2 = 1;
        adj[other[a*2+c1]].insert(b*2+c2); adj[other[b*2+c2]].insert(a*2+c1);
        rev[b*2+c2].insert(other[a*2+c1]); rev[a*2+c1].insert(other[b*2+c2]);
    }
    memset(visited, false, sizeof(visited));
    for(int i = 0; i < n; i ++)
        if(!visited[i])
            dfs1(i);
    memset(scc, 0, sizeof(scc));
    while(!ord.empty())
    {
        if(!scc[ord.top()])
        {
            dfs2(ord.top());
            curCC ++;
        }
        ord.pop();
    }
    for(int i = 0; i < m*2; i += 2)
        if(scc[i] == scc[i+1] && scc[i] != 0)
        {
            cout << "IMPOSSIBLE\n"; return 0;
        }
    for(int i = 0; i < m*2; i ++)
        for(auto it : rev[i])
        {
            if(scc[it] != scc[i])
            {
                if(adj2[scc[i]].find(scc[it]) == adj2[scc[i]].end()) in[scc[it]] ++;
                adj2[scc[i]].insert(scc[it]);
            }
        }
    for(int i = 1; i < curCC; i ++)
        if(in[i] == 0)
            q.push(i);
    memset(ans, 'a', sizeof(ans));
    while(!q.empty())
    {
        cur = q.front(); q.pop();
        for(auto it : sc[cur])
            if(ans[it/2] == 'a')
            {
                if(it % 2 == 0) ans[it/2] = '+';
                else ans[it/2] = '-';
            }
        for(auto it : adj2[cur])
        {
            in[it] --;
            if(!in[it]) q.push(it);
        }
    }
    for(int i = 0; i < m; i ++)
        cout << ((ans[i]=='a')?'+':ans[i]) << " ";
    cout << "\n";
}
*/


ll mod = 1000000007;
ll nums[200010];
ll fac[200010];
ll pw[40];
ll cnt[40];
map<ll, ll> m;
int main()
{
    int t, n;
    ll cur, ans;
    cin >> t;
    pw[0] = 1;
    for(int i = 1; i < 31; i ++)
        pw[i] = pw[i-1]*2;
    fac[0] = 1;
    for(int i = 1; i < 200010; i ++)
        fac[i] = (fac[i-1] * i) % mod;
    while(t--)
    {
        cin >> n;
        m.clear();
        for(int i = 0; i < 40; i ++)
            cnt[i] = 0;
        for(int i = 0; i < n; i ++)
        {
            cin >> nums[i];
            for(int j = 0; j < 31; j ++)
                if((pw[j] & nums[i]) == 0)
                    cnt[j] ++;
        }
        cur = 0;
        for(int i = 0; i < 31; i ++)
            if(cnt[i] == 0)
                cur += pw[i];
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < 31; j ++)
                if((pw[j] & nums[i]) == 0)
                {
                    cnt[j] --;
                    if(cnt[j] == 0)
                        cur += pw[j];
                }
            if(cur == nums[i]) m[cur] ++;
            for(int j = 0; j < 31; j ++)
                if((pw[j] & nums[i]) == 0)
                {
                    cnt[j] ++;
                    if(cnt[j] == 1)
                        cur -= pw[j];
                }
        }
        ans = 0;
        for(auto it : m)
            ans = (ans + ((it.s * (it.s-1)) % mod * fac[n-2]) % mod) % mod;
        cout << ans%mod << "\n";
    }
}
