#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mod = 1e9 + 7;

int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++) {
        res *= i;
        res %= mod;
    }
    return res % mod;
}

int binpow(int a, int b)
{
    if (b == 0)
        return 1;
    if (b % 2 == 0) {
        int c = binpow(a, b / 2);
        return c * c % mod;
    }
    return a * binpow(a, b - 1) % mod;
}

int Cnk(int n, int k)
{
    return fact(n) * binpow(fact(k) * fact(n - k) % mod, mod - 2) % mod;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[n];
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        int mn = *min_element(a, a + n);
        int res = mn;
        for (int i = 0; i < n; i++) {
            res &= a[i];
        }
        if (res != mn) {
            cout << 0 << endl;
            continue;
        }
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt += (a[i] == mn);
        }
        if (cnt < 2)
            cout << 0 << endl;
        else
            cout << Cnk(cnt, 2) * fact(n - 2) % mod * 2 % mod << endl;
    }

    return 0;
}