A sequence of $n$ non-negative integers ($n \ge 2$) $a_1, a_2, \dots, a_n$ is called good if for all $i$ from $1$ to $n-1$ the following condition holds true: $$a_1 \: \& \: a_2 \: \& \: \dots \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: \dots \: \& \: a_n,$$ where $\&$ denotes the bitwise AND operation .

You are given an array $a$ of size $n$ ($n \geq 2$). Find the number of permutations $p$ of numbers ranging from $1$ to $n$, for which the sequence $a_{p_1}$, $a_{p_2}$, ... ,$a_{p_n}$ is good. Since this number can be large, output it modulo $10^9+7$.


-----Input-----

The first line contains a single integer $t$ ($1 \leq t \leq 10^4$), denoting the number of test cases.

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the size of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the array.

It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.


-----Output-----

Output $t$ lines, where the $i$-th line contains the number of good permutations in the $i$-th test case modulo $10^9 + 7$.


-----Examples-----

Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4


-----Note-----

In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of $6$ permutations possible with numbers from $1$ to $3$: $[1,2,3]$, $[1,3,2]$, $[2,1,3]$, $[2,3,1]$, $[3,1,2]$, $[3,2,1]$.

In the second test case, it can be proved that no permutation exists for which the sequence is good.

In the third test case, there are a total of $36$ permutations for which the sequence is good. One of them is the permutation $[1,5,4,2,3]$ which results in the sequence $s=[0,0,3,2,0]$. This is a good sequence because

$ s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0$,

$ s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0$,

$ s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0$,

$ s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0$.

Difficulty: MEDIUM
Tags: Bit manipulation

Combinatorics

Mathematics

Constructive algorithms
Skill types: Bit manipulation

Starter code:

Solutions:
MOD = int(1000000000.0 + 7)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	cnt = 0
	ok = True
	for i in range(n):
		if mn & arr[i] != mn:
			ok = False
			break
		if arr[i] == mn:
			cnt += 1
	if ok:
		ans = cnt * (cnt - 1)
		for i in range(2, n - 1):
			ans = ans * i % MOD
		print(ans % MOD)
	else:
		print(0)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_print_int(x):
	sys.stdout.write(str(x) + '\n')

def get_print(x):
	sys.stdout.write(x + '\n')

def get_print_int_same(x):
	sys.stdout.write(str(x) + ' ')

def get_print_same(x):
	sys.stdout.write(x + ' ')
from sys import maxsize

def solve():
	for _ in range(get_int()):
		n = get_int()
		arr = get_list()
		MOD = 1000000007
		res = arr[0]
		for i in arr:
			res &= i
		count = 0
		for i in range(n):
			if arr[i] == res:
				count += 1
		ans = count * (count - 1)
		for i in range(2, n - 1):
			ans = ans * i % MOD
		get_print_int(ans)
solve()


import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7

def factorial(n):
	ans = 1
	for i in range(2, n + 1):
		ans = ans * i % mod
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	minn = min(a)
	(cnt, flag) = (0, 1)
	for i in a:
		if i == minn:
			cnt += 1
		if i & minn != minn:
			flag = 0
	ans = 0
	if flag:
		ans = cnt * (cnt - 1) * factorial(n - 2) % mod
	print(ans)


mod = 1000000007
tm = 200005
fact = [i for i in range(tm)]
for i in range(2, tm):
	fact[i] = fact[i - 1] % mod * (fact[i] % mod) % mod
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	x = arr[0]
	for i in range(1, n):
		x = x & arr[i]
	y = arr.count(x)
	ot = n - 2
	if y <= 1:
		print(0)
	elif y == 2:
		if n == 2:
			print(2)
		else:
			print(fact[ot] % mod * (2 % mod) % mod)
	else:
		sn = y * (y - 1)
		print(fact[ot] % mod * (sn % mod) % mod)


MOD = 7 + 10 ** 9

def solution():
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	mc = 0
	tp = 1
	for i in range(n):
		if i + 1 <= n - 2:
			tp = tp * (i + 1) % MOD
		if arr[i] == mn:
			mc += 1
		if arr[i] & mn != mn:
			print(0)
			return
	if mc < 2:
		print(0)
		return
	ans = mc * (mc - 1) * tp % MOD
	print(ans)
t = int(input())
while t:
	t -= 1
	solution()


N = 1000005
mod = 10 ** 9 + 7
fac = [1, 1]
for i in range(2, N):
	fac.append(fac[i - 1] * i % mod)
for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	cnt = 0
	for i in range(n):
		if l[i] == l[0]:
			cnt += 1
		if l[i] & l[0] != l[0]:
			print(0)
			break
	else:
		print(fac[n - 2] * cnt * (cnt - 1) % mod)


import sys, math, itertools, random, bisect
from collections import defaultdict
INF = 10 ** 18

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	store = [0] * 30
	for i in range(30):
		for j in range(n):
			if a[j] & 1 << i:
				store[i] += 1
	key = 0
	for i in range(30):
		if store[i] == n:
			key |= 1 << i
	cnt = a.count(key)
	if cnt <= 1:
		print(0)
	else:
		ans = cnt % mod * ((cnt - 1) % mod) % mod * (math.factorial(n - 2) % mod) % mod
		print(ans % mod)


import sys
from collections import deque, defaultdict
from itertools import permutations
md = 10 ** 9 + 7
f = [1] * (2 * 10 ** 5 + 7)
for j in range(1, 2 * 10 ** 5 + 3):
	f[j] = f[j - 1] % md * (j + 1) % md % md
t = int(input())
for i in range(t):
	n = int(input())
	d = list(map(int, input().split()))
	p = d[0]
	for i in d:
		p &= i
	t = 0
	for j in d:
		if j == p:
			t += 1
	print(t * (t - 1) % md * (f[n - 3] % md) % md)


from sys import stdin
input = stdin.readline
mod = 10 ** 9 + 7
for test in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	bit = lst[0]
	for i in lst[1:]:
		bit &= i
	x = lst.count(bit)
	fact = 1
	for i in range(2, n - 1):
		fact = fact * i % mod
	print(x * (x - 1) * fact % mod)


import sys
import os.path
if os.path.exists('input_file.txt'):
	sys.stdin = open('input_file.txt', 'r')
	sys.stdout = open('output_file.txt', 'w')
mod = 1000000007

def factorial(a):
	ans = 1
	for i in range(1, a + 1):
		ans = ans * i % mod
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(an, c) = (a[0], 0)
	for i in a:
		an &= i
	for i in a:
		if i == an:
			c += 1
	if c == 1 or c == 0:
		print(0)
	else:
		if c == n:
			ans = factorial(n) % mod
		else:
			ans = factorial(n - 2) * c * (c - 1) % mod
		print(ans)


m = 1000000007
for _ in range(int(input())):
	n = int(input())
	l = tuple(map(int, input().split()))
	a = l[0]
	for i in l[1:]:
		a &= i
	t = l.count(a)
	if t > 1:
		p = 1
		for i in range(2, n - 1):
			p *= i
			p %= m
		s = t * (t - 1) * p % m
		print(s)
	else:
		print(0)


factorials = [1] * 200001
mod = 1000000007
for i in range(1, 200001):
	factorials[i] = factorials[i - 1] % mod * i % mod % mod
from math import comb
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	minnElement = min(arr)
	k = arr.count(minnElement)
	if k < 2:
		print(0)
		continue
	else:
		And = arr[0]
		for i in range(1, n):
			And &= arr[i]
		if And == minnElement:
			print(2 * (comb(k, 2) * factorials[n - 2]) % mod)
		else:
			print(0)
			continue


MOD = 10 ** 9 + 7
fac = [1]
for i in range(2 * 10 ** 5):
	fac.append(fac[-1] * (i + 1) % MOD)
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ands = a[0]
	put = 0
	for i in range(1, n):
		temp = ands & a[i]
		if temp != ands:
			put = -1
			break
	if a.count(a[0]) < 2 or put == -1:
		print(0)
		continue
	if len(set(a)) == 1:
		print(fac[n] % MOD % MOD)
		continue
	else:
		x = a.count(a[0])
		ans = x * (x - 1) % MOD
		ans = ans % MOD * (fac[n - 2] % MOD) % MOD
		print(ans % MOD)


MOD = 10 ** 9 + 7

def f(n):
	fact = 1
	for i in range(1, n + 1):
		fact = fact * i % MOD
	return fact
t = int(input())
for asdfasdf in range(0, t):
	l = int(input())
	a = list(map(int, input().split()))
	u = a[0]
	ans = 0
	for i in a:
		u &= i
	s = {}
	perm_no_double = f(len(a) - 2)
	for i in a:
		s.update({i: s.get(i, 0) + 1})
	for (key, value) in s.items():
		if value > 1 and u & key == key:
			ans += perm_no_double * value * (value - 1)
	print(ans % MOD)


from sys import stdin
input = stdin.readline

def f(a):
	mod = 10 ** 9 + 7
	bit = a[0]
	for i in a[1:]:
		bit &= i
	a = list(map(lambda s: s - bit, a))
	cntz = a.count(0)
	if cntz == 0:
		return 0
	ans = cntz * (cntz - 1) % mod
	for i in range(1, len(a) - 1):
		ans = ans * i % mod
	return ans
for i in range(int(input())):
	a = input()
	lst = list(map(int, input().strip().split()))
	print(f(lst))


import math
farr = [1]

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	res = arr[0]
	for i in arr:
		res = res & i
	ct = arr.count(res)
	remain = n - 2
	result = 0
	modval = 10 ** 9 + 7
	if ct > 1:
		npr = ct * (ct - 1)
		f = npr
		s = fact(int(remain), modval)
		result = f * s % modval
	print(int(result))


mod = 10 ** 9 + 7
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	x = arr[0]
	for i in range(n - 1):
		x = x & arr[i]
	x1 = arr[1]
	for i in range(1, n):
		x1 = arr[i] & x1
	if x != x1:
		print(0)
	else:
		x_count = arr.count(x)
		if x_count >= 2:
			ans = x_count * (x_count - 1)
			for i in range(1, n - 1):
				ans = ans % mod * (i % mod) % mod
			print(ans)
		else:
			print(0)


def pre(n, mod):
	dp = [0] * (n + 1)
	dp[0] = 1
	for i in range(1, n + 1):
		dp[i] = dp[i - 1] * i
		dp[i] %= mod
	return dp

def solve(arr, n, facts, mod, ans):
	annd = pow(2, 32) - 1
	counts = {}
	for i in arr:
		annd = annd & i
		if i not in counts.keys():
			counts[i] = 0
		counts[i] += 1
	total = 0
	if annd in counts.keys():
		total += counts[annd] * (counts[annd] - 1)
	total *= facts[n - 2]
	total %= mod
	ans.append(str(total))

def main():
	mod = pow(10, 9) + 7
	facts = pre(2 * pow(10, 5), mod)
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(arr, n, facts, mod, ans)
	print('\n'.join(ans))
main()


import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	tot_and = a[0]
	for i in range(1, n):
		tot_and &= a[i]
	mn_count = 0
	for i in a:
		if i == mn:
			mn_count += 1
	if tot_and != mn or mn_count == 1:
		print(0)
	else:
		fact = [1] * n
		for i in range(2, n):
			fact[i] = fact[i - 1] * i % mod
		print(mn_count * (mn_count - 1) * fact[n - 2] % mod)


import math
from collections import Counter

def most(List):
	cnt = 0
	occurence_count = dict(Counter(List))
	for (k, v) in occurence_count.items():
		if v >= 2:
			cnt += v
	return cnt

def most_frequent(List):
	cnt = 0
	occurence_count = Counter(List)
	return occurence_count.most_common(1)[0][0]
fac = [1]
MOD = 10 ** 9 + 7
for i in range(2 * 10 ** 5):
	fac.append(fac[-1] * (i + 1) % MOD)
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ands = a[0]
	put = 0
	for i in range(1, n):
		temp = ands & a[i]
		if temp != ands:
			put = -1
			break
	if n == 2:
		if a[0] == a[1]:
			print(2)
		else:
			print(0)
		continue
	if a.count(a[0]) < 2:
		print(0)
		continue
	if put == -1:
		print(0)
		continue
	if len(set(a)) == 1:
		print(fac[n] % MOD % MOD)
		continue
	if a.count(0) >= 2:
		x = a.count(0)
		ans = x * (x - 1) % MOD
		ans = ans % MOD * (fac[n - 2] % MOD) % MOD
		print(ans % MOD)
		continue
	else:
		x = a.count(a[0])
		ans = x * (x - 1) % MOD
		ans = ans % MOD * (fac[n - 2] % MOD) % MOD
		print(ans % MOD)


import sys
import math
from math import factorial, inf, gcd
from heapq import *
from functools import *
from itertools import *
from collections import *
from typing import *
sys.setrecursionlimit(10 ** 5)

def rarray():
	return [int(i) for i in input().split()]
mod = 10 ** 9 + 7
ti = 1
ti = int(input())
for ii in range(ti):
	n = int(input())
	a = rarray()
	s = (1 << 33) - 1
	c = Counter(a)
	for i in a:
		s &= i
	if c[s] >= 2:
		ans = c[s] * (c[s] - 1) % mod
		for i in range(n - 2):
			ans = ans * (n - 2 - i) % mod
		print(ans)
	else:
		print(0)


MOD = 10 ** 9 + 7

def factorialModulo(n):
	f = 1
	for i in range(1, n + 1):
		f = f * i % MOD
	return f

def solve(A, n):
	freq = {}
	andProduct = A[0]
	for a in A:
		freq[a] = freq.get(a, 0) + 1
		andProduct &= a
	if andProduct in freq and freq[andProduct] > 1:
		return freq[andProduct] * (freq[andProduct] - 1) * factorialModulo(n - 2) % MOD
	else:
		return 0
t = int(input())
for tc in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	result = solve(A, n)
	print(result)


t = int(input())

def main(a):
	a.sort()
	cnt = 1
	for e in a[1:]:
		if e == a[0]:
			cnt += 1
		elif a[0] & e != a[0]:
			return 0
	if cnt < 2:
		return 0
	res = 1
	for i in range(2, len(a) - 1):
		res = res * i % 1000000007
	res = res * (cnt - 1) % 1000000007
	res = res * cnt % 1000000007
	return res
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	print(main(a))


M = 10 ** 9 + 7
fact = [1] * (2 * 10 ** 5)
for i in range(1, len(fact)):
	fact[i] = fact[i - 1] * i % M
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = arr[0] & arr[1]
	for i in range(2, n - 1):
		mn &= arr[i]
	mn &= arr[-1]
	dup = arr.count(mn)
	if dup < 2:
		print(0)
		continue
	print(fact[n - 2] * dup * (dup - 1) % M)
dim = [1, 2, 3]

def index(*args):
	m = dim[0]
	s = args[-1]
	j = 1
	for i in args[-2::-1]:
		s += m * i
		m *= dim[j]
		j += 1
	return s


mod = pow(10, 9) + 7
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	val = arr[0]
	for i in range(1, n):
		val = val & arr[i]
	val2 = arr.count(val)
	if val2 < 2:
		print(0)
		continue
	ans = val2 * (val2 - 1) % mod
	fac = 1
	for i in range(1, n - 1):
		fac = fac * i % mod
	ans = ans * fac % mod
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = a[0]
	for x in a:
		k = k & x
	r = a.count(k)
	r = r * (r - 1)
	for i in range(2, len(a) - 1):
		r = r * i % (10 ** 9 + 7)
	print(r)


import os
import sys
from io import BytesIO, IOBase
import math
from collections import Counter

def func(n, array):
	curr_and = array[0]
	min_val = array[0]
	count_min = 1
	for i in array[1:]:
		if i < min_val:
			min_val = i
			count_min = 1
		elif i == min_val:
			count_min += 1
		curr_and = curr_and & i
	if curr_and < min_val:
		return 0
	return count_min * (count_min - 1) * math.factorial(n - 2) % (10 ** 9 + 7)

def main():
	num_test = int(parse_input())
	result = []
	for _ in range(num_test):
		n = int(parse_input())
		array = [int(i) for i in parse_input().split()]
		result.append(func(n, array))
	print('\n'.join(map(str, result)))
parse_input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import io, os
from collections import Counter
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ok = [True for i in range(n)]
	for p in range(0, 32):
		mask = 1 << p
		bits = [1 if v & mask else 0 for v in a]
		c = Counter(bits)
		if c[0] == 0:
			pass
		elif c[0] == 1:
			for i in range(n):
				ok[i] = False
		else:
			for i in range(n):
				if bits[i] == 1:
					ok[i] = False
	x = sum((1 for v in ok if v))
	y = n - x
	if x == 0:
		print(0)
		continue
	res = x * (x - 1) % 1000000007
	for i in range(n - 2, 0, -1):
		res = res * i % 1000000007
	print(res)


import sys
input = sys.stdin.readline
from collections import Counter
from math import sqrt
from functools import reduce
mod = 10 ** 9 + 7

def cal(x):
	ans = set()
	for i in range(29, -1, -1):
		if x >> i & 1:
			ans.add(i)
	return ans

def perm(n, m):
	ans = 1
	for i in range(n, n - m, -1):
		ans = ans * i % mod
	return ans

def fac(x):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	cnt = A.count(0)
	if cnt == 1:
		print(0)
		continue
	if cnt >= 2:
		ans = perm(cnt, 2) * fac(n - 2) % mod
		print(ans)
		continue
	S = [cal(a) for a in A]
	x = reduce(lambda x, y: x & y, S)
	cnt1 = 0
	for s in S:
		if s & x == s:
			cnt1 += 1
	ans = perm(cnt1, 2) * fac(n - 2) % mod
	print(ans)


import math

def read_ints():
	return [int(i) for i in input().split()]

def solve(nums):
	res = nums[0]
	potential_res = 10 ** 9 + 1
	res_count = 0
	for num in nums:
		res = res & num
		if res != potential_res:
			potential_res = res
			res_count = 0
		if num == potential_res:
			res_count += 1
	res = res_count * (res_count - 1)
	for i in range(1, len(nums) - 1):
		res = res * i % (10 ** 9 + 7)
	return res
T = int(input())
for t in range(1, T + 1):
	N = int(input())
	nums = read_ints()
	answer = solve(nums)
	print('{1}'.format(t, answer))


import math

def read_ints():
	return [int(i) for i in input().split()]

def solve(nums):
	res = nums[0]
	for num in nums:
		res = res & num
	res_count = len(list(filter(lambda a: a == res, nums)))
	return res_count * (res_count - 1) * math.factorial(len(nums) - 2) % (10 ** 9 + 7)
T = int(input())
for t in range(1, T + 1):
	N = int(input())
	nums = read_ints()
	answer = solve(nums)
	print('{1}'.format(t, answer))


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	a = l[0]
	for i in range(1, n):
		a = a & l[i]
	if l.count(a) == 1:
		print(0)
		continue
	b = l.count(a)
	b = b * (b - 1) % (10 ** 9 + 7)
	for i in range(1, n - 1):
		b = b * (i % (10 ** 9 + 7)) % (10 ** 9 + 7)
	print(b)


M = 10 ** 9 + 7
f = [1]
for i in range(1, 4 ** 9):
	f += (f[-1] * i % M,)
for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	k = -1
	for x in a:
		k &= x
	m = a.count(k)
	print(m * (m - 1) * f[len(a) - 2] % M)


for _ in range(int(input())):
	x = int(input())
	zoop = list(map(int, input().split()))
	mod = pow(10, 9) + 7
	zz = min(zoop)
	cont = True
	cnt = 0
	for i in range(len(zoop)):
		if zoop[i] == zz:
			cnt += 1
		elif zoop[i] & zz != zz:
			print(0)
			cont = False
			break
	if cont:
		fact = 1
		for i in range(1, len(zoop) - 1):
			fact *= i
			fact %= mod
		fact *= cnt * (cnt - 1)
		fact %= mod
		print(fact)


from collections import Counter
import math

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		f = a[0]
		for i in range(1, n):
			f = f & a[i]
		c = 0
		for j in range(n):
			if f == a[j]:
				c += 1
		if c == 0 or c == 1:
			print(0)
		else:
			res = c * (c - 1)
			for k in range(2, n - 1):
				res *= k
				if res > 1000000006:
					res = res % 1000000007
			print(res)
main()


t = int(input())
for i in range(t):
	n = int(input())
	line = input()
	line = line.split(' ')
	nmin = 10 ** 10 + 100
	count = 0
	for num in line:
		if int(num) <= nmin:
			nmin = int(num)
	invalid = False
	for num in line:
		if int(num) == nmin:
			count += 1
		if int(num) & nmin != nmin:
			invalid = True
			break
	if invalid or count < 2:
		print(0)
		continue
	mod = 10 ** 9 + 7
	out = 1
	for i in range(1, n - 1):
		out = out * i % mod
	ans = count * (count - 1) % mod
	out = out * ans % mod
	print(int(out))


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import defaultdict, Counter, OrderedDict, deque
from heapq import heapify, heappush, heappop
from bisect import *
from operator import mul
from itertools import permutations
(maxx, mod) = (float('inf'), int(1000000000.0 + 7))
(localsys, start_time) = (0, time.time())
if path.exists('input.txt'):
	localsys = 1
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = a[0]
	for i in a:
		t &= i
	(cnt, ans) = (a.count(t), 0)
	ans += cnt * (cnt - 1) % mod
	for i in range(n - 2, 0, -1):
		ans = ans * i % mod
	print(ans)
if localsys:
	print('\n\n\nTime Elased :', time.time() - start_time, 'seconds')


t = int(input())
MOD = 10 ** 9 + 7

def nCr(n, r):
	x = 1
	y = 1
	for i in range(r):
		x *= n - i
		y *= i + 1
		x %= MOD
		y %= MOD
	return x * pow(y, MOD - 2, MOD) % MOD

def nPr(n, r):
	x = 1
	for i in range(r):
		x *= n - i
		x %= MOD
	return x
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = [True] * n
	for i in range(30):
		if all((a[j] >> i & 1 for j in range(n))):
			continue
		for j in range(n):
			if a[j] >> i & 1:
				f[j] = False
	c = f.count(True)
	ans = 1
	ans *= nPr(c, 2)
	ans %= MOD
	ans *= nPr(n - 2, n - 2)
	ans %= MOD
	print(ans)


from math import factorial
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = arr[0]
	for i in range(n):
		temp &= arr[i]
	mn = arr.count(temp)
	if mn < 2:
		print(0)
		continue
	ans = mn * (mn - 1)
	fs = 1
	for i in range(2, n - 1):
		fs = fs * i % mod
	print(fs * ans % mod)


mod = 10 ** 9 + 7
fac = [0] * 200003
fac[0] = 1
for i in range(1, 200003):
	fac[i] = fac[i - 1] * i % mod
for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	v = [0] * 34
	for i in a:
		try:
			m[i] += 1
		except:
			m[i] = 1
		j = 0
		while i > 0:
			if i & 1 == 1:
				v[j] += 1
			j += 1
			i //= 2
	common = 0
	for i in range(34):
		if v[i] == n:
			common += 2 ** i
	try:
		ans = m[common] * (m[common] - 1) % mod
		ans = ans * fac[n - 2] % mod
		print(ans)
	except:
		print(0)


for test in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = a[0]
	for x in a:
		k &= x
	r = a.count(k)
	r *= r - 1
	for i in range(2, len(a) - 1):
		r = r * i % (10 ** 9 + 7)
	print(r)


import os, io, sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
MOD = 10 ** 9 + 7
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = a[0]
	for i in range(n):
		k = a[i] & k
	count = 0
	for i in range(n):
		a[i] -= k
		if a[i] == 0:
			count += 1
	if count < 2:
		print(0)
		continue
	ans = count * (count - 1) % MOD
	for i in range(n - 2, 0, -1):
		ans = ans * i % MOD
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	x = arr[0]
	for i in range(n):
		x = x & arr[i]
	a = 0
	if x != arr[0] or x != arr[1]:
		print(0)
	else:
		for i in range(n):
			if x == arr[i]:
				a += 1
		ans = a * (a - 1) % (10 ** 9 + 7)
		for i in range(1, n - 1):
			ans = ans * i % (10 ** 9 + 7)
		print(ans)


import sys
import collections
input = sys.stdin.readline
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

def main():
	global fact, factinv, inv
	MOD = 10 ** 9 + 7
	for i in range(2, 2 * 10 ** 5 + 2):
		fact.append(fact[-1] * i % MOD)
		inv.append(-inv[MOD % i] * (MOD // i) % MOD)
		factinv.append(factinv[-1] * inv[-1] % MOD)
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		c = collections.Counter(A)
		mi = min(c.keys())
		if c[mi] <= 1:
			print(0)
			continue
		f = True
		for a in A:
			if a & mi != mi:
				f = False
				break
		if not f:
			print(0)
			continue
		ans = c[mi] * (c[mi] - 1)
		ans *= fact[N - 2]
		print(ans % MOD)
main()


import sys
from collections import *
from heapq import *
import math
import bisect

def input():
	return sys.stdin.readline()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	minn = min(a)
	cnt = a.count(minn)
	ans = True
	ss = a[0]
	for i in a:
		ss = ss & i
	if cnt < 2 or ss != minn:
		print(0)
	else:
		summ = 0
		summ += cnt * (cnt - 1)
		summ *= math.factorial(n - 2)
		print(summ % 1000000007)


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, log2, sqrt, gcd
import bisect
import sys
import copy

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
p = 10 ** 9 + 7

def fact(n, p):
	ans = 1
	while n > 0:
		ans = ans * n
		ans %= p
		n -= 1
	return ans % p
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	ans = a[0]
	for i in range(1, len(a)):
		ans &= a[i]
	cnt = a.count(ans)
	if cnt < 2:
		print(0)
	else:
		ans1 = cnt % p * ((cnt - 1) % p) % p
		ans1 = ans1 * fact(n - 2, p) % p
		print(ans1)


MOD = pow(10, 9) + 7

def factorial(num):
	x = 1
	for s in range(num, 0, -1):
		x = x * s % MOD
	return x
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == 1:
		print(factorial(n) % MOD)
	else:
		min_A = min(a)
		Correct = True
		for i in range(n):
			if a[i] & min_A != min_A:
				Correct = False
				break
		if not Correct:
			print(0)
			continue
		n_zeros = a.count(min_A)
		print(n_zeros * (n_zeros - 1) * factorial(n - 2) % MOD)


import math
T = int(input())
for t in range(1, T + 1):
	N = int(input())
	A = list(map(int, input().split()))
	full_and = A[0]
	for n in A[1:]:
		full_and &= n
	occ = A.count(full_and)
	print(occ * (occ - 1) * math.factorial(N - 2) % (10 ** 9 + 7))


import sys
m = int(1000000000.0 + 7)

def npr(n, r):
	x = 1
	for i in range(n, n - r, -1):
		x = x * i % m
	return x
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = sys.maxsize
	for i in l:
		x &= i
	co = 0
	for i in l:
		if i == x:
			co += 1
	if co >= 2:
		ans = npr(co, 2) * npr(n - 2, n - 2) % m
		print(ans)
	else:
		print(0)


from math import *
from collections import *
from functools import *
from bisect import *
from itertools import *
from heapq import *
inf = float('inf')
ninf = -float('inf')
ip = input
alphal = 'abcdefghijklmnopqrstuvwxyz'
alphau = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mod = 10 ** 9 + 7

def ipl():
	return list(map(int, ip().split()))

def ipn():
	return int(ip())

def ipf():
	return float(ip())

def solve():
	n = ipn()
	a = ipl()
	m = a[0]
	for i in a:
		m &= i
	r = a.count(m)
	r *= r - 1
	for i in range(1, n - 1):
		r = r * i % mod
	print(r % mod)
t = ipn()
for _ in range(t):
	solve()


import math
from operator import iand
from functools import reduce
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 1000000007
	res = reduce(lambda x, y: x & y, a)
	m = a.count(res)
	k = 1
	i = 1
	while i < n - 1:
		k = k * i % c
		i += 1
	k = k * m * (m - 1)
	print(k % c)


import sys
import collections
import math
import bisect
import heapq
inf = sys.maxsize

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = get_array()
	m = collections.Counter(arr)
	totand = arr[0]
	for i in range(n):
		totand &= arr[i]
	if len(m) == 1:
		ans = 1
		for i in range(1, n + 1):
			ans *= i
			ans %= mod
		print(ans)
	else:
		v = []
		for i in range(n):
			if arr[i] == totand:
				v.append(arr[i])
		if len(v) <= 1:
			print(0)
		else:
			ans = len(v)
			ans = ans * (ans - 1) % mod
			ans2 = 1
			for i in range(1, n - 1):
				ans2 *= i
				ans2 %= mod
			tot = ans * ans2 % mod
			print(tot)


mod = int(1000000000.0 + 7)

def jc(n):
	s = int(1)
	for i in range(1, n + 1):
		s *= i
		s %= mod
	return s
t = int(input())
while t > 0:
	n = int(input())
	b = input()
	a = [int(d) for d in b.split()]
	k = a[0]
	for i in a:
		k &= i
	m = int(0)
	for i in a:
		if i == k:
			m = m + 1
	f = int(jc(n - 2))
	ans = int(m * (m - 1) % mod * f % mod)
	print(ans)
	t -= 1


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def modFact(n, p):
	if n >= p:
		return 0
	result = 1
	for i in range(1, n + 1):
		result = result * i % p
	return result

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = ii()
	a = li()
	if len(set(a)) == n:
		print(0)
		return
	a.sort()
	minEl = a[0]
	f = 0
	for i in a:
		if minEl & i == minEl:
			continue
		else:
			print(0)
			return
	minCnt = a.count(minEl)
	ways = minCnt * (minCnt - 1) // 2
	res = modFact(n - 2, 1000000007)
	p = 1000000007
	ways = ways % p
	ways = ways * res % p
	ways = ways * 2
	ways = ways % p
	print(ways)
t = 1
t = int(input())
for _ in range(t):
	solve()


import bisect
import collections
import functools
import itertools
import math
import heapq
import random
import string

def repeat(_func=None, *, times=1):

	def decorator(func):

		@functools.wraps(func)
		def wrapper(*args, **kwargs):
			for _ in range(times):
				func(*args, **kwargs)
		return wrapper
	if _func is None:
		return decorator
	else:
		return decorator(_func)

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def getint():
	return int(input())

def getmatrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def display_matrix(mat):
	for i in range(len(mat)):
		print(mat[i])

@repeat(times=int(input()))
def main():
	n = getint()
	arr = l_unpack()
	m = min(arr)
	cnt = arr.count(m)
	flag = False
	for c in arr:
		if m & c == m:
			continue
		flag = True
		break
	if cnt < 2 or flag:
		print(0)
		return
	ans = cnt * (cnt - 1) % MOD
	for i in range(n - 2, 1, -1):
		ans *= i
		ans %= MOD
	print(ans)
MOD = 1000000007
main()


from sys import stdin, stdout, maxsize
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))
M = int(1000000000.0) + 7

def fact(n):
	ans = 1
	for i in range(1, n + 1):
		ans = ans * i % M % M
	return ans

def mode_inv(n):
	return pow(n, M - 2, M)

def npr(n, r):
	return fact(n) * mode_inv(fact(n - r)) % M
for tc in range(int(R())):
	n = int(R())
	a = RL(int)
	bin_a = [(35 - len(bin(i)[2:])) * '0' + bin(i)[2:] for i in a]
	s = set(list(range(n)))
	for i in range(35):
		cnt1 = 0
		for j in range(n):
			if int(bin_a[j][i]):
				cnt1 += 1
		if cnt1 == n:
			continue
		for j in range(n):
			if int(bin_a[j][i]) and j in s:
				s.remove(j)
	if len(s) < 2:
		output(0)
		continue
	output(npr(len(s), 2) * fact(n - 2) % M)


import math
import sys

def answer(n, a):
	mn = min(a)
	cmn = a.count(mn)
	if cmn < 2:
		return 0
	mnperm = cmn * (cmn - 1)
	mdperm = 1
	if n - 2 > 0:
		for i in range(2, n - 1):
			mdperm = mdperm * i % (10 ** 9 + 7)
	if mn == 0:
		return mnperm * mdperm % (10 ** 9 + 7)
	for i in range(n):
		if a[i] & mn != mn:
			return 0
	return mnperm * mdperm % (10 ** 9 + 7)

def main():
	t = int(input())
	while t:
		n = int(sys.stdin.readline())
		a = [int(i) for i in sys.stdin.readline().split()]
		print(answer(n, a))
		t -= 1
	return
main()


mod = int(1000000000.0 + 7)

def fact(x):
	f = 1
	for i in range(2, x + 1):
		f *= i
		f %= mod
	return f
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	if a[0] != a[1]:
		print(0)
		continue
	else:
		tot = a.count(a[0])
		t = 1
		for i in range(2, n):
			if a[0] & a[i] != a[0]:
				t = 0
				break
		if t == 0:
			print(0)
		else:
			res = int(fact(n - 2) * (tot * (tot - 1)))
			res %= mod
			print(res)


def factorial(n):
	prod = 1
	for i in range(1, n + 1, 1):
		prod = prod * i % 1000000007
	return prod
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	min = arr[0]
	andprod = arr[0]
	count = 1
	for i in range(1, n, 1):
		andprod = andprod & arr[i]
		if arr[i] < min:
			min = arr[i]
			count = 1
		elif arr[i] == min:
			count = count + 1
	if count < 2 or andprod != min:
		print('0')
	else:
		sum = count * (count - 1) % 1000000007
		sum = sum * factorial(n - 2) % 1000000007
		print(sum)


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd, sqrt, factorial
from itertools import permutations
for _ in range(it()):
	n = it()
	l = mp()
	mi = min(l)
	count = l.count(mi)
	flag = 0
	for i in range(n):
		if mi != mi & l[i]:
			flag = 1
			print(0)
			break
	mod = 10 ** 9 + 7
	if not flag:
		ans = count * (count - 1) * (factorial(n - 2) % mod)
		print(ans % mod)


import sys
import math
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	data = list(map(int, sys.stdin.readline().split()))
	val = min(data)
	(cnt, flag) = (0, 0)
	for item in data:
		if val == item:
			cnt += 1
			continue
		if val & item != val:
			flag = 1
			break
	if flag:
		print(0)
		continue
	ans = cnt * (cnt - 1)
	for i in range(1, n - 1):
		ans = ans * i % int(math.pow(10, 9) + 7)
	print(ans)


t = int(input())
factorials = {}
modulo_value = 10 ** 9 + 7

def factorial(n):
	if n == 1:
		return 1
	fact = 1
	for i in range(1, n + 1):
		fact = fact * i % modulo_value
	return fact % modulo_value

def combonations2(n):
	return int(n * (n - 1) / 2)

def is_ok(n, array):
	for i in array:
		if i & n != n:
			return False
	return True
for _ in range(t):
	n = int(input())
	array = [int(a) for a in input().split(' ')]
	d = dict()
	for a in array:
		if a not in d:
			d[a] = 0
		d[a] += 1
	starters = []
	for k in d:
		if d[k] > 1:
			starters.append([k, d[k]])
	if len(starters) == 0:
		print(0)
		continue
	internal_pemrutations = factorial(n - 2)
	result = 0
	for s in starters:
		if is_ok(s[0], array):
			result += combonations2(s[1])
	print(2 * result * internal_pemrutations % modulo_value)


import os, sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	mod = 10 ** 9 + 7
	fac = [1]
	for i in range(1, 2 * 10 ** 5 + 1):
		fac.append(fac[-1] * i % mod)
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		x = Counter(a)
		an = a[0]
		for i in a:
			an &= i
		z = x[an]
		ans = z * (z - 1)
		print(ans * fac[n - 2] % mod)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	anda = a[0]
	for j in range(1, n):
		anda = anda & a[j]
	c = 0
	for j in range(n):
		if a[j] == anda:
			c = c + 1
	if c == 0 or c == 1:
		print('0')
	else:
		b = 1
		for j in range(n - 2):
			b = b * (j + 1) % 1000000007
		b = b * c * (c - 1) % 1000000007
		print(b)


import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
mv = 2 * 10 ** 5
A1 = [1]
v = 1
for i in range(1, mv + 1):
	v = v * i % mod
	A1.append(v)
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	v = min(A)
	cnt = 0
	flag = 0
	for i in range(n):
		if A[i] == v:
			cnt = cnt + 1
		elif A[i] & v != v:
			flag = 1
			break
	if cnt < 2 or flag == 1:
		print(0)
	else:
		ans = cnt * (cnt - 1) % mod
		ans = ans * A1[n - 2] % mod
		print(ans)


import collections
import sys
import math
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from functools import cmp_to_key
from collections import defaultdict
from collections import deque
import heapq

def solve():
	t = int(input())
	mod = 10 ** 9 + 7
	while t:
		t -= 1
		n = int(input())
		a = list(map(int, input().split()))
		mn = min(a)
		mp = defaultdict(int)
		res = mn
		for num in a:
			mp[num] += 1
			res &= num
		if mp[mn] < 2 or res != mn:
			print(0)
			continue
		fact = 1
		for i in range(1, n - 1):
			fact = fact * i % mod
		ans = mp[mn] * (mp[mn] - 1) % mod
		print(ans * fact % mod)

def main():
	solve()
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = l[0]
	a = l[1]
	for i in range(2, n):
		a = a & l[i]
	if a == m:
		c = l.count(m)
		if c == 1:
			print(0)
		else:
			ans = 1
			ans = c * (c - 1)
			for i in range(n - 2):
				ans = ans * (i + 1) % 1000000007
			print(ans % 1000000007)
	else:
		print(0)


import math
mod = 1000000007
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	temp = li[0]
	for i in li[1:]:
		temp = temp & i
	x = li.count(temp)
	if x < 2:
		print(0)
	else:
		print(x * (x - 1) * math.factorial(n - 2) % mod)


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
mxm = sys.maxsize
from collections import defaultdict, Counter

def factorial(n):
	dp = [1] * (n + 1)
	for i in range(2, n):
		dp[i] = dp[i - 1] * i % mod
	return dp

def main():
	fact = factorial(2 * 10 ** 5)
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		countmm = 0
		mm = mxm
		for item in l:
			if item < mm:
				mm = item
				countmm = 1
			elif item == mm:
				countmm += 1
		if countmm < 2:
			print(0)
			continue
		nd = mm
		flag = 0
		for item in l:
			if item & nd == nd:
				pass
			else:
				flag = 1
				break
		if flag:
			print(0)
		else:
			print(countmm * (countmm - 1) * fact[n - 2] % mod)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


def fact(n):
	p = 1
	for j in range(1, n + 1):
		p *= j
		p %= 1000000007
	return p
t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	mi = min(lst)
	c = lst.count(mi)
	if c < 2:
		print(0)
	else:
		lst.remove(mi)
		lst.remove(mi)
		e = 0
		for j in lst:
			if mi & j != mi:
				e += 1
				break
		if e > 0:
			print(0)
		else:
			a = c * (c - 1)
			b = fact(n - 2)
			ans = a * b
			print(ans % 1000000007)


def helper(nums):
	mod = 1000000007
	gand = nums[0]
	for i in range(1, len(nums)):
		gand = gand & nums[i]
	k = nums.count(gand)
	if k < 2:
		return 0
	res = k * (k - 1)
	curr = len(nums) - 2
	while curr > 1:
		res = res * curr % mod
		curr -= 1
	return res
t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	print(helper(nums))


import sys
input = sys.stdin.readline

def I():
	return input().strip()

def II():
	return int(input().strip())

def LI():
	return [*map(int, input().strip().split())]
import string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict
from itertools import permutations, combinations, groupby
MOD = 10 ** 9 + 7

def nc2(a):
	return int(a * (a - 1) / 2) % MOD

def fac(n):
	if n <= 0:
		return 1
	ans = 1
	for i in range(1, n + 1):
		ans *= i
		ans %= MOD
	return ans
for _ in range(II()):
	n = II()
	a = LI()
	m = min(a)
	mc = a.count(m)
	c = 0
	for i in a:
		if m & i == m:
			c += 1
	ans = 0
	if c == n:
		ans = 2 * nc2(mc) * fac(n - 2) % MOD
	print(ans)


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()
mod = 10 ** 9 + 7

def case():
	n = IN()
	a = LI()
	m = min(a)
	x = a.count(m)
	an = a[0]
	for i in range(1, n):
		an &= a[i]
	if an != m or x == 1:
		print(0)
	else:
		ans = x % mod * ((x - 1) % mod)
		ans %= mod
		for i in range(1, n - 1):
			ans *= i
			ans %= mod
		ans %= mod
		print(ans)
for _ in range(IN()):
	case()


def fact(n, p):
	ans = 1
	for i in range(1, n + 1):
		ans = ans * i % p
	return ans
for _ in range(int(input())):
	p = int(1000000000.0) + 7
	n = int(input())
	lis = list(map(int, input().split()))
	a = lis[0]
	for i in range(1, n):
		a &= lis[i]
	if a in lis and lis.count(a) > 1:
		cnt = lis.count(a)
		ans = cnt * (cnt - 1) % p
		ans = ans * fact(n - 2, p) % p
		print(ans)
	else:
		print(0)


def compute(n):
	k = 1
	for i in range(1, n - 2 + 1):
		k *= i
		k %= 1000000007
	return k
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	p = s[0]
	for i in range(len(s)):
		p &= s[i]
	cnt = s.count(p)
	if cnt < 2:
		print(0)
	else:
		print(cnt * (cnt - 1) * compute(n) % 1000000007)


mod = 10 ** 9 + 7
temp = [1, 1]
for j in range(2, 2 * 10 ** 5 + 1):
	temp.append(temp[-1] * j % mod)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	total = l[0]
	for j in range(1, n):
		total = total & l[j]
	val = l.count(total)
	if val < 2:
		print(0)
		continue
	for j in range(0, n):
		if l[j] & total != total:
			flag = 1
			break
	if flag == 1:
		print(0)
	else:
		lol = val * (val - 1) % mod
		final = lol * temp[n - 2] % mod
		print(final)


import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def dijkstra(start, distance, path, n):
	visited = [False for _ in range(n)]
	distance[start] = 0
	for i in range(n):
		v = -1
		for j in range(n):
			if not visited[v] and (v == -1 or distance[j] < distance[v]):
				v = j
		if distance[v] == math.inf:
			break
		visited[v] = True
		for edge in adj[v]:
			destination = edge[0]
			weight = edge[1]
			if distance[v] + weight < distance[destination]:
				distance[destination] = distance[v] + weight
				path[destination] = v

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def seive(n):
	primes = [True] * (n + 1)
	ans = []
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	for p in range(2, n + 1):
		if primes[p]:
			ans += [p]
	return ans

def factors(n):
	factors = []
	x = 1
	while x * x <= n:
		if n % x == 0:
			if n // x == x:
				factors.append(x)
			else:
				factors.append(x)
				factors.append(n // x)
		x += 1
	return factors

def main():
	try:
		from collections import Counter
		for _ in range(inp()):
			n = inp()
			a = inlt()
			val = a[0]
			for i in range(1, n):
				val &= a[i]
			c = Counter(a)
			if val != min(a) or c[val] < 2:
				print(0)
				continue
			ans = math.factorial(n - 2) * c[val] * (c[val] - 1)
			ans %= 10 ** 9 + 7
			print(ans)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[0]
	for i in range(n):
		b = b & a[i]
	cb = 0
	for i in range(n):
		if a[i] == b:
			cb += 1
	if cb < 2:
		print(0)
	else:
		k = cb * (cb - 1)
		for i in range(n - 2):
			k = k * (i + 1) % mod
		print(k)


from functools import reduce
from sys import stdin
input = stdin.readline

def main():
	MOD = int(1000000000.0 + 7)
	test = int(input())
	for _ in range(test):
		n = int(input())
		ara = [int(x) for x in input().split()]
		all_and = reduce(lambda x, y: x & y, ara)
		count = 0
		for num in ara:
			if num == all_and:
				count += 1
		ans = count * (count - 1) % MOD
		for i in range(n - 2):
			ans = ans * (i + 1) % MOD
		print(ans)
main()


from itertools import permutations as perm
from collections import Counter
LIM = 2 * 10 ** 5 + 10
f = [1] * LIM
MOD = 10 ** 9 + 7
for i in range(1, LIM):
	f[i] = i * f[i - 1] % MOD

def check(li):
	f = [li[0]]
	for it in li[1:]:
		f.append(f[-1] & it)
	s = [li[-1]]
	for it in li[-2::-1]:
		s.append(s[-1] & it)
	s.reverse()
	for i in range(len(li) - 1):
		if f[i] != s[i + 1]:
			return False
	return True

def get(p, arr):
	li = [arr[ind] for ind in p]
	return li

def brute(arr):
	ln = len(arr)
	ans = 0
	for p in perm(range(ln)):
		li = get(p, arr)
		if check(li):
			print(li)
			ans += 1
	print(ans)

def solve(A, n):
	And = A[0]
	for it in A[1:]:
		And &= it
	C = Counter(A)
	c = C[And]
	if c < 2:
		return 0
	ans = c * (c - 1) * f[n - 2] % MOD
	return ans
for case in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ans = solve(A, n)
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	import bisect
	import math
	alpha = 'abcdefghijklmnopqrstuvwxyz'
	ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	inf = 1e+17
	mod = 10 ** 9 + 7

	def factorial(n):
		M = 1000000007
		f = 1
		for i in range(1, n + 1):
			f = f * i % M
		return f

	def ncr(n, r, p):
		num = den = 1
		for i in range(r):
			num = num * (n - i) % p
			den = den * (i + 1) % p
		return num * pow(den, p - 2, p) % p

	def solve(n, arr):
		Min = min(arr)
		cnt = arr.count(Min)
		for ele in arr:
			if ele & Min != Min:
				return 0
		if cnt >= 0:
			const = factorial(n - 2)
			answer = cnt % mod * ((cnt - 1) % mod) * const % mod
			return answer % mod
		return 0
	t = int(input())
	ans = []
	for _ in range(t):
		n = int(input())
		arr = [int(x) for x in input().split()]
		ans.append(solve(n, arr))
	for test in ans:
		print(test)
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


MOD = 1000 * 1000 * 1000 + 7

def and_array(array):
	result = array[0]
	for i in range(1, len(array)):
		result = result & array[i]
	return result

def mul(a, b):
	return a * b % MOD

def fact(n):
	result = 1
	for i in range(1, n + 1):
		result = mul(result, i)
	return result

def _pow(a, p):
	if p == 0:
		return 1
	if p == 1:
		return a
	if p % 2 == 1:
		return mul(_pow(a, p - 1), a)
	else:
		result = _pow(a, p // 2)
		return mul(result, result)

def solve(n, array):
	_and = and_array(array)
	_and_count = sum((1 if x == _and else 0 for x in array))
	if _and_count < 2:
		return 0
	return mul(mul(_and_count, _and_count - 1), fact(n - 2))

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		array = [int(x) for x in input().split()]
		print(solve(n, array))
main()


p = 10 ** 9 + 7

def modFact(n):
	p = 10 ** 9 + 7
	if n >= p:
		return 0
	result = 1
	for i in range(1, n + 1):
		result = result * i % p
	return result
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict(Counter(a))
	b = a[0]
	for i in range(1, n):
		b = b & a[i]
	z = min(a)
	if b != z or d[z] < 2:
		print(0)
	else:
		k = d[z]
		ans = k * (k - 1)
		ans = ans * modFact(n - 2)
		print(ans % p)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def A(n):
	return [0] * n

def AI(n, x):
	return [x] * n

def A2(n, m):
	return [[0] * m for i in range(n)]

def G(n):
	return [[] for i in range(n)]

def GP(it):
	return [[ch, len(list(g))] for (ch, g) in groupby(it)]
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
mod = 10 ** 9 + 7
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	fact(x, mod)
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa.reverse()

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def floorsum(a, b, c, n):
	if a == 0:
		return b // c * (n + 1)
	if a >= c or b >= c:
		return floorsum(a % c, b % c, c, n) + b // c * (n + 1) + a // c * n * (n + 1) // 2
	m = (a * n + b) // c
	return n * m - floorsum(c, c - b - 1, a, m - 1)

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

def lowbit(n):
	return n & -n

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class ST:

	def __init__(self, arr):
		n = len(arr)
		mx = n.bit_length()
		self.st = [[0] * mx for i in range(n)]
		for i in range(n):
			self.st[i][0] = arr[i]
		for j in range(1, mx):
			for i in range(n - (1 << j) + 1):
				self.st[i][j] = max(self.st[i][j - 1], self.st[i + (1 << j - 1)][j - 1])

	def query(self, l, r):
		if l > r:
			return -inf
		s = (r + 1 - l).bit_length() - 1
		return max(self.st[l][s], self.st[r - (1 << s) + 1][s])

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		seen.add(u)
		for (v, w) in graph[u]:
			if v not in d or d[v] > d[u] + w:
				d[v] = d[u] + w
				heappush(heap, (d[v], v))
	return d

def bell(s, g):
	dis = AI(n, inf)
	dis[s] = 0
	for i in range(n - 1):
		for (u, v, w) in edge:
			if dis[v] > dis[u] + w:
				dis[v] = dis[u] + w
	change = A(n)
	for i in range(n):
		for (u, v, w) in edge:
			if dis[v] > dis[u] + w:
				dis[v] = dis[u] + w
				change[v] = 1
	return dis

def lcm(a, b):
	return a * b // gcd(a, b)

def lis(nums):
	res = []
	for k in nums:
		i = bisect.bisect_left(res, k)
		if i == len(res):
			res.append(k)
		else:
			res[i] = k
	return len(res)

def RP(nums):
	n = len(nums)
	s = set(nums)
	d = {}
	for (i, k) in enumerate(sorted(s), 1):
		d[k] = i
	bi = BIT([0] * (len(s) + 1))
	ans = 0
	for i in range(n - 1, -1, -1):
		ans += bi.query(d[nums[i]] - 1)
		bi.update(d[nums[i]], 1)
	return ans

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None

def nb(i, j, n, m):
	for (ni, nj) in [[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]:
		if 0 <= ni < n and 0 <= nj < m:
			yield (ni, nj)

def topo(n):
	q = deque()
	res = []
	for i in range(1, n + 1):
		if ind[i] == 0:
			q.append(i)
			res.append(i)
	while q:
		u = q.popleft()
		for v in g[u]:
			ind[v] -= 1
			if ind[v] == 0:
				q.append(v)
				res.append(v)
	return res

@bootstrap
def gdfs(r, p):
	for ch in g[r]:
		if ch != p:
			yield gdfs(ch, r)
	yield None

@bootstrap
def dfs(r, p, path):
	path[c[r]] += 1
	if path[c[r]] == 1:
		ans.append(r + 1)
	for ch in g[r]:
		if ch != p:
			yield dfs(ch, r, path)
	path[c[r]] -= 1
	yield None
ifact(2 * 10 ** 5, mod)
t = N()
for i in range(t):
	n = N()
	a = RLL()
	c = Counter(a)
	mi = min(a)
	if c[mi] < 2:
		ans = 0
	else:
		f = False
		for i in range(n):
			if a[i] & mi != mi:
				f = True
				ans = 0
				break
		if not f:
			ans = per(c[mi], 2, mod) * per(n - 2, n - 2, mod) % mod
	print(ans)


t = int(input())
f = [0] * 10 ** 6
f[0] = f[1] = 1
mod = 10 ** 9 + 7
for i in range(2, 10 ** 6):
	f[i] = f[i - 1] * i % mod
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	for i in range(1, n):
		x &= a[i]
	ans = 0
	c = [0] * n
	for i in range(n):
		if a[i] == x & a[i]:
			c[i] = 1
	cc = sum(c)
	for i in range(n):
		if c[i] > 0:
			ans += (cc - c[i]) * f[n - 2]
			ans %= mod
	print(ans)


from sys import stdin, stdout
DIV = int(1000000000.0 + 7)
list_factorials = [1]

def get_fact(n):
	if len(list_factorials) - 1 < n:
		for i in range(len(list_factorials), n + 1):
			list_factorials.append(int(list_factorials[-1] * i % DIV))
	return list_factorials[n]

def get_num_perm(seq):
	cnt = int(seq[0])
	for i in range(1, len(seq)):
		cnt &= seq[i]
	cnt_fit = 0
	for i in range(0, len(seq)):
		if seq[i] == cnt:
			cnt_fit += 1
	first = cnt_fit * (cnt_fit - 1) % DIV
	second = get_fact(len(seq) - 2)
	ans = first * second % DIV
	stdout.write(str(int(ans)) + '\n')
num = int(stdin.readline())
for _ in range(num):
	stdin.readline()
	seq = [int(el) for el in stdin.readline().split()]
	get_num_perm(seq)


from collections import Counter
import string
import math
import bisect
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 10 ** 9 + 7
fact = [1]
for i in range(1, 2 * 10 ** 5 + 1):
	fact.append(fact[-1] * i % mod)
testcases = vary(1)
for _ in range(testcases):
	n = vary(1)
	num = array_int()
	xt = num[0]
	for i in range(n):
		xt &= num[i]
	count = num.count(xt)
	print(count * (count - 1) % mod * fact[n - 2] % mod)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
N = 200015
fct = l1d(N, 1)
for i in range(1, N):
	fct[i] = fct[i - 1] * i % mod
for _ in range(int(inp())):
	n = iinp()
	arr = lmp()
	c = arr[0]
	for i in arr:
		c &= i
	ct = 0
	for i in arr:
		ct += 1 if c == i else 0
	if ct < 2:
		print(0)
		continue
	print(ct * (ct - 1) * fct[n - 2] % mod)


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, gcd
import bisect
import sys
import copy

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
f = [1] * 200001
for i in range(2, 200001):
	f[i] = f[i - 1] * i % mod
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	and_all = a[0]
	for i in range(1, n):
		and_all = and_all & a[i]
	cnt = a.count(and_all)
	if cnt >= 2:
		print(f[n - 2] * cnt * (cnt - 1) % mod)
	else:
		print(0)


t = int(input())
mod = 10 ** 9 + 7
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	num = arr[0]
	for i in range(n):
		num = num & arr[i]
	count = 0
	for i in range(n):
		if num ^ arr[i] == 0:
			count += 1
	if count == 1 or count == 0:
		print(0)
	else:
		ans = count * (count - 1) % mod
		for i in range(1, n - 1):
			ans = ans * i % mod
		print(ans)


from sys import stdin, stdout, setrecursionlimit
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, log2
from random import seed, randint
from datetime import datetime
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
from collections import defaultdict as dd
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def def_value():
	return 0

def def_inf():
	return inf

def atoi(s):
	return ord(s) - ord('a')

def inc(s, n=1):
	return chr((ord(s) - ord('a') + n) % 26 + ord('a'))
for _ in range(iinp()):
	n = iinp()
	a = lmp()
	mn = min(a)
	num = a.count(mn)
	b = mn
	for i in range(n):
		b &= a[i]
	if num >= 2 and b == mn:
		ans = 1
		for i in range(1, n - 1):
			ans = ans * i % mod
		sel = num * (num - 1)
		ans = ans * sel % mod
		print(ans)
	else:
		print(0)


from math import *
from collections import *
for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	for i in l:
		d[i] += 1
	x = l[0]
	for i in range(1, n):
		x = x & l[i]
	s = 0
	if d[x] >= 2:
		s = d[x] * (d[x] - 1) * factorial(n - 2)
	print(s % (10 ** 9 + 7))


def li():
	return [int(i) for i in input().split()]

def ii():
	return int(input())

def bin32(num):
	s = '{0:032b}'.format(num)
	x = ''
	for i in range(31, -1, -1):
		x += s[i]
	return x
fact = [1]
fac = 1
MOD = 1000000007
for i in range(1, 200010):
	fac = fac * i % MOD
	fact.append(fac)
INF = 100000000000000
for t in range(ii()):
	n = ii()
	a = li()
	andd = a[0]
	for i in a:
		andd &= i
	cnt = 0
	for i in a:
		if i == andd:
			cnt += 1
	print(cnt * (cnt - 1) * fact[n - 2] % MOD)


import math

def solve(n, a):
	cnt = 0
	d = a[0]
	for x in a[1:]:
		d = d & x
	for x in a:
		if x == d:
			cnt += 1
	if cnt < 2:
		return 0
	base = cnt * (cnt - 1) % (7 + 10 ** 9)
	for i in range(1, n - 1):
		base = base * i % (7 + 10 ** 9)
	return base
tt = int(input())
while tt > 0:
	tt -= 1
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


import sys

def readnum():
	input = sys.stdin.readline
	a = list(map(int, input().split()))
	return a

def printf(val):
	sys.stdout.write(str(val) + '\n')

def power(a, b, mod):
	if b == 1:
		return a
	if b == 0:
		return 1
	ans = power(a, b // 2, mod)
	ans *= ans
	if b % 2:
		ans *= a
	ans = ans % mod
	return ans
t = readnum()[0]

def factorial(n, start, skip, mod):
	res = start
	for i in range(1, n + 1):
		if i == skip:
			continue
		res *= i
		res = res % mod
	return res
for _ in range(t):
	n = readnum()[0]
	arr = readnum()
	res = arr[0]
	for i in range(n):
		res = res & arr[i]
	m = sum([i == res for i in arr])
	if m < 2:
		print(0)
	else:
		print(factorial(n - 2, 1, None, 10 ** 9 + 7) * m * (m - 1) % (10 ** 9 + 7))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	summ = a[0]
	for i in range(1, n):
		summ &= a[i]
	count = 0
	for i in range(n):
		if a[i] == summ:
			count += 1
	if count == 0 or count == 1:
		print('0')
	else:
		ans = 1
		for i in range(n - 2):
			ans *= i + 1
			ans %= 1000000007
		ans *= count * (count - 1)
		ans %= 1000000007
		print(ans)


mod = 10 ** 9 + 7
mxn = 2 * 10 ** 5 + 10
fac = [1] * mxn
for i in range(2, mxn):
	fac[i] = fac[i - 1] * i
	fac[i] %= mod

def c(n, r):
	return n * (n - 1) // 2
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	p = l[0]
	for i in range(n):
		p &= l[i]
	s = 0
	for i in range(n):
		if l[i] == p:
			s += 1
	print(2 * c(s, 2) * fac[n - 2] % (10 ** 9 + 7))


from math import *
N = int(1000000000.0 + 7)
f = []

def fact():
	global f
	f.append(int(1))
	for i in range(1, 200005):
		f.append(int(f[len(f) - 1] * i % N))
fact()
for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	p = int(2 ** 31 - 1)
	c = []
	for i in range(0, n):
		c.append(int(0))
	for i in range(0, n):
		x = arr[i]
		p &= arr[i]
		for j in range(0, 31):
			if x % 2 == 0:
				c[i] += 1
			x //= 2
	cnt = int(0)
	cnt1 = int(0)
	while p > 0:
		if p % 2 == 1:
			cnt += 1
		p //= 2
	for i in range(0, n):
		if c[i] == 31 - cnt:
			cnt1 += 1
	if cnt1 <= 1:
		print(0)
		continue
	ans = cnt1 * (cnt1 - 1)
	ans %= N
	ans *= f[n - 2]
	ans %= N
	print(int(ans))


import math
import itertools
from collections import defaultdict
DEBUG = True

def log(*args, **kwargs):
	if DEBUG:
		print(*args, **kwargs)

def ri():
	return int(input())

def rl(f=int):
	return list(map(f, input().split()))

def rs():
	return input()

class Solution:

	def __init__(self):
		pass

	def run(self):
		n = ri()
		a = rl()
		bits = [0] * 32
		d = defaultdict(int)
		for x in a:
			for i in range(31):
				bits[i] += x >> i & 1
			d[x] += 1
		result = 0
		for (k, v) in d.items():
			if v > 1:
				ok = True
				for i in range(31):
					if k >> i & 1 == 1 and bits[i] < n:
						ok = False
						break
				if ok:
					result += v * (v - 1) * math.factorial(n - 2)
		return result % (10 ** 9 + 7)

	def c(self, n, r):
		return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def test():
	n = 5
	a = [1, 3, 5, 7, 1]
	result = 0
	for p in itertools.permutations(a):
		ok = True
		for i in range(1, n):
			left = p[:i]
			right = p[i:]
			l = 2 ** 32 - 1
			r = 2 ** 32 - 1
			for x in left:
				l &= x
			for x in right:
				r &= x
			if l != r:
				ok = False
				break
		if ok:
			result += 1
	print('done', result)
t = int(input())
s = Solution()
for i in range(t):
	print(s.run())


mod = 10 ** 9 + 7
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	f = 0
	for i in range(n):
		if m & a[i] != m:
			f = 1
			break
	c = a.count(m)
	if c == 1 or f:
		print(0)
	else:
		ans = c * (c - 1) % mod
		for i in range(n - 2, 0, -1):
			ans = ans * i % mod
		print(ans)


import sys
input = sys.stdin.readline
rem = 1000000007
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ma = max(ar)
	req = []
	st = 1
	while st <= ma:
		count = 0
		for i in range(n):
			if ar[i] & st:
				count += 1
		if count != n and count != 0:
			req.append(st)
		st <<= 1
	mai = 0
	for i in range(n):
		flag = True
		for j in req:
			if ar[i] & j:
				flag = False
		if flag:
			mai += 1
	fac = n - 2
	ans = mai * (mai - 1) // 2
	ans = ans % rem
	ans = ans * 2 % rem
	for i in range(1, fac + 1):
		ans = ans * i % rem
	print(ans)


for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	k = -1
	for x in a:
		k &= x
	r = a.count(k)
	r *= r - 1
	for i in range(2, len(a) - 1):
		r = r * i % (10 ** 9 + 7)
	print(r)


from math import factorial
M = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = arr[0] & arr[1]
	for i in range(2, n):
		mn &= arr[i]
	dup = arr.count(mn)
	if dup < 2:
		print(0)
		continue
	print(factorial(n - 2) % M * dup * (dup - 1) % M)
dim = [1, 2, 3]

def index(*args):
	m = dim[0]
	s = args[-1]
	j = 1
	for i in args[-2::-1]:
		s += m * i
		m *= dim[j]
		j += 1
	return s


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def Solution(arr, n):
	mod = pow(10, 9) + 7
	mn = min(arr)
	cnt = 0
	for v in arr:
		if v == mn:
			cnt += 1
		if mn & v != mn:
			print(0)
			return
	fact = 1
	for i in range(1, n - 1):
		fact = fact % mod * (i % mod) % mod
	ans = cnt * (cnt - 1) % mod
	ans = ans % mod * (fact % mod) % mod
	print(ans)

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = get_ints_in_list()
		Solution(arr, n)
main()


mod = 10 ** 9 + 7
import math

def nPr(n, r):
	return (n - 1) * n % mod

def modFact(n):
	p = mod
	if n >= p:
		return 0
	result = 1
	for i in range(1, n + 1):
		result = result * i % p
	return result
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	an = ar[0]
	for i in range(n):
		an &= ar[i]
	cnt = ar.count(an)
	if cnt == 0:
		print(0)
		continue
	fact = modFact(n - 2)
	print(fact * nPr(cnt, 2) % mod)


for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	x = ar[0]
	for elem in ar:
		x &= elem
	num = 0
	for elem in ar:
		if elem == x:
			num += 1
	if num == 1:
		print(0)
	else:
		MOD = 10 ** 9 + 7
		ans = num * (num - 1)
		for i in range(1, n - 1):
			ans *= i
			ans %= MOD
		print(ans)


mod = 1000000007
for i in range(int(input())):
	n = int(input())
	d = list(map(int, input().split()))
	s = d[0]
	for j in range(1, len(d)):
		s &= d[j]
	c = 0
	for j in d:
		c += j == s
	if c < 2:
		print(0)
	else:
		fc = 1
		for j in range(2, n - 1):
			fc *= j
			fc %= mod
		print(c * (c - 1) * fc % mod)


mod = 1000000007
for _ in range(int(input())):
	num_count = int(input())
	nums = list(map(int, input().split()))
	m = min(nums)
	ct = nums.count(m)
	if ct < 2 or any((n & m != m for n in nums)):
		print(0)
	else:
		f = 1
		for i in range(1, num_count - 1):
			f = f * i % mod
		ans = ct * (ct - 1) % mod
		ans = ans * f % mod
		print(int(ans))


import io
import os
from collections import Counter, defaultdict, deque
MAX_N = 3 * 10 ** 5
MOD = 10 ** 9 + 7

def modInverse(a, p):
	return pow(a, p - 2, p)
fact = [1]
for i in range(1, MAX_N + 1):
	fact.append(fact[-1] * i % MOD)
invFact = [0] * (MAX_N + 1)
invFact[MAX_N] = modInverse(fact[MAX_N], MOD)
for i in range(MAX_N - 1, -1, -1):
	invFact[i] = invFact[i + 1] * (i + 1) % MOD

def nCr(n, r):
	if n < r:
		return 0
	return fact[n] * invFact[r] * invFact[n - r] % MOD

def solveWA(N, A):
	bad = set()
	for pos in range(32):
		ones = 0
		zeroes = 0
		for x in A:
			if 1 << pos & x:
				ones += 1
			else:
				zeroes += 1
		if ones == N or zeroes == N:
			continue
		for (i, x) in enumerate(A):
			if 1 << pos & x:
				bad.add(i)
	good = N - len(bad)
	return nCr(good, 2) * 2 * fact[N - 2] % MOD

def solve(N, A):
	for pos in range(32):
		ones = 0
		zeroes = 0
		for x in A:
			if x >> pos & 1:
				ones += 1
			else:
				zeroes += 1
		if ones == N:
			for i in range(N):
				A[i] -= 1 << pos
	good = A.count(0)
	return nCr(good, 2) * 2 * fact[N - 2] % MOD

def solve1(N, A):
	import itertools
	check = 0
	for perm in itertools.permutations(A):
		for i in range(1, N):
			front = (1 << 32) - 1
			back = (1 << 32) - 1
			for x in perm[:i]:
				front &= x
			for x in perm[i:]:
				back &= x
			if front != back:
				break
		else:
			check += 1
	check %= MOD
	return check
DEBUG = False
if DEBUG:
	import random
	random.seed(0)
	for _ in range(1000000000):
		N = random.randint(2, 8)
		A = [random.randint(0, 10) for i in range(N)]
		A = [10 ** 9] * N
		print('tc' + str(_), N, A)
		ans = solve(N, A)
		ans1 = solve1(N, A)
		print(A)
		print(ans, ans1)
		assert ans == ans1
	exit()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


from collections import *
mod = 7 + 10 ** 9

def fac(t):
	ans = 1
	for i in range(2, t + 1):
		ans = ans * i % mod
	return ans

def fun(t):
	return t * (t - 1) // 2 % mod
TT = int(input())
for y in range(TT):
	n = int(input())
	lst = list(map(int, input().split()))
	if n == 2:
		if lst[0] == lst[1]:
			print('2')
		else:
			print('0')
		continue
	ad = lst[0]
	for i in range(n):
		ad = ad & lst[i]
	cnt = lst.count(ad)
	if cnt < 2:
		print('0')
	else:
		print(2 * fun(cnt) * fac(n - 2) % mod)


m = 10 ** 9 + 7
fact = [0] * (2 * 10 ** 5 + 3)
fact[0] = 1
fact[1] = 1
for i in range(2, 2 * 10 ** 5 + 3):
	fact[i] = i * fact[i - 1] % m
import math
t = int(input())
for i in range(t):
	n = int(input())
	z = list(map(int, input().split()))
	p = min(z)
	c = 0
	for i in z:
		if i == p:
			c += 1
		if p & i != p:
			print(0)
			break
	else:
		ans = c * (c - 1) * fact[n - 2] % m
		print(ans)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
m = int(1000000000.0 + 7)

def fact(n):
	ans = 1
	for i in range(1, n + 1):
		ans = ans * i % m
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = max(a)
	for i in a:
		c &= i
	v = a.count(c)
	if v > 1:
		print(fact(n - 2) % m * v * (v - 1) % m)
	else:
		print('0')


import math, sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, values) = (int(input()), [int(i) for i in input().split()])
	(low, cnt, ans) = (min(values), values.count(min(values)), float('inf'))
	for i in range(n):
		if low & values[i] != low:
			ans = 0
	ans = min(ans, cnt * (cnt - 1) * math.factorial(n - 2) % (10 ** 9 + 7))
	print(ans)


t = int(input())
for tt in range(t):
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	ans = 0
	mod = int(1000000000.0 + 7)
	s = a[0]
	cnt = 0
	for i in range(1, n):
		s &= a[i]
	for i in range(n):
		if a[i] == s:
			cnt += 1
	if cnt < 2:
		print(ans)
		continue
	ans = cnt * (cnt - 1) % mod
	tmp = n - 2
	while tmp > 0:
		ans = ans * tmp % mod
		tmp -= 1
	print(ans)


mod = 1000000007

def A(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i % mod
	return res
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	andres = a[0]
	for i in a:
		andres = andres & i
	cnt = 0
	for i in a:
		if i == andres:
			cnt += 1
	if cnt < 2:
		print(0)
		continue
	cnt = cnt % mod
	print(cnt * (cnt - 1) * A(n - 2) % mod)


from functools import lru_cache
import sys
import math
mod = 10 ** 9 + 7
inf = float('inf')

def arrInt():
	return list(map(int, input().split()))

def solve(n, arr):

	def fact(n):
		fact = 1
		for i in range(1, n + 1):
			fact = fact * i % mod
		return fact
	and_el = arr[0]
	for el in arr:
		and_el &= el
	count = 0
	for el in arr:
		count += int(el == and_el)
	count = count * (count - 1) % mod
	ans = fact(n - 2) * count % mod
	print(ans)
if 1 == 2:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	arr = arrInt()
	solve(n, arr)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	same = 0
	p = l[0]
	for i in range(1, n):
		p = p & l[i]
	if p != l[0] or l[0] != l[1]:
		print(0)
	else:
		for i in range(n):
			if l[i] == l[0]:
				same += 1
		z = n - 2
		ans = 1
		ans *= same
		ans *= same - 1
		ans = ans % 1000000007
		for i in range(1, z + 1):
			ans *= i
			ans = ans % 1000000007
		print(ans)


def factorial(n):
	fact = 1
	for i in range(1, n + 1):
		fact *= i
		fact %= 10 ** 9 + 7
	return fact
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = min(arr)
	flag = True
	c = 0
	for j in range(len(arr)):
		if arr[j] == mini:
			c += 1
		elif arr[j] & mini != mini:
			flag = False
	if c < 2 or not flag:
		print(0)
	else:
		c = c % (10 ** 9 + 7)
		print(c * (c - 1) * factorial(n - 2) % (10 ** 9 + 7))


def solve():
	mod = int(1000000000.0 + 7)
	n = int(input())
	a = list(map(int, input().split()))
	min1 = min(a)
	cnt = 0
	for x in a:
		if min1 == x:
			cnt = cnt + 1
		if min1 & x != min1:
			print(0)
			return
	fact = 1
	for i in range(1, n - 1):
		fact = fact * i % mod
	ans = cnt * (cnt - 1) % mod
	ans = ans * fact % mod
	print(ans)
for t in range(int(input())):
	solve()


def check(m):
	theta = 0
	prefix = m[:]
	theta = 0
	for i in range(1, n):
		theta = 0
		prefix[i] &= prefix[i - 1]
		theta = 0
	theta = 0
	suffix = m[:]
	theta = 0
	i = n - 2
	theta = 0
	while i >= 0:
		theta = 0
		suffix[i] &= suffix[i + 1]
		theta = 0
		i -= 1
	theta = 0
	for i in range(1, n):
		theta = 0
		if prefix[i - 1] == suffix[i]:
			theta = 0
			continue
			theta = 0
		else:
			return 0
	theta = 0
	return 1
for i in range(int(input())):
	n = int(input())
	theta = 0
	l = list(map(int, input().split()))
	theta = 0
	a = min(l)
	k = l.count(a)
	theta = 0
	if k >= 2:
		pass
	else:
		print(0)
		continue
	theta = 0
	l.remove(a)
	theta = 0
	l.remove(a)
	m = [a] + l + [a]
	theta = 0
	answer = k * (k - 1)
	theta = 0
	if not check(m):
		print(0)
		continue
	theta = 0
	mod = int(pow(10, 9) + 7)
	theta = 0
	for i in range(1, n - 1):
		answer *= i
		answer %= mod
	theta = 0
	print(answer)


for _ in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	s.sort()
	a1 = s[0]
	d = dict()
	d1 = dict()
	d[s[0]] = 1
	for i in range(1, n):
		if s[i] in d:
			d[s[i]] += 1
			d1[s[i]] = d[s[i]]
		else:
			d[s[i]] = 1
		a1 = a1 & s[i]
	if a1 in d1:
		ans = d1[a1] * (d1[a1] - 1) % 1000000007
		for i in range(1, n - 1):
			ans = ans * i % 1000000007
		print(ans)
	else:
		print(0)


from math import factorial

def allahmiyabharose(a, n):
	temp = a[0]
	for i in range(n):
		temp &= a[i]
	main = a.count(temp)
	if main < 2:
		print(0)
		return ''
	else:
		print(factorial(n - 2) * main * (main - 1) % 1000000007)
		return ''
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	allahmiyabharose(a, n)


import sys, os
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
tc = 1
tc = int(input())
for test in range(1, tc + 1):
	n = int(input())
	a = inp()
	ans = a[0]
	for i in a:
		ans = i & ans
	c = a.count(ans)
	ans = 0
	if c > 1:
		ans = c * (c - 1) % mod
		for i in range(1, n - 1):
			ans = ans * i % mod
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	an = a[0]
	for i in a[1:]:
		an &= i
	c = a.count(an)
	fact = 1
	for i in range(2, n - 1):
		fact = fact * i % 1000000007
	print(c * (c - 1) * fact % 1000000007)


MOD = 10 ** 9 + 7

def fact(n):
	r = 1
	for i in range(2, n + 1):
		r *= i
		r %= MOD
	return r

def solve(n, a):
	mask = a[0]
	for e in a[1:]:
		mask &= e
	cnt = 0
	for e in a:
		if e & ~mask == 0:
			cnt += 1
	if cnt < 2:
		return 0
	res = cnt * (cnt - 1) % MOD
	res *= fact(n - 2)
	res %= MOD
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


MOD = 10 ** 9 + 7
MAX = 200005
fact = [1]
for zz in range(1, MAX + 1):
	fact.append(fact[-1] % MOD * zz % MOD % MOD)

def checkP(p):
	n = len(p)
	for i in range(n - 1):
		left = 2 ** 30 - 1
		for j in range(i + 1):
			left &= p[j]
		right = 2 ** 30 - 1
		for j in range(i + 1, n):
			right &= p[j]
		if left != right:
			return False
	return True

def bruteforce(a):
	a = readIntArr()
	n = len(a)
	from itertools import permutations
	ans = []
	for p in permutations(a):
		if checkP(p):
			ans.append(p)
	return len(ans)

def main():
	t = int(input())
	allans = []
	for _ in range(t):
		n = int(input())
		a = readIntArr()
		cumu = ~0
		for x in a:
			cumu &= x
		cnt = 0
		for x in a:
			if x == cumu:
				cnt += 1
		if cnt <= 1:
			ans = 0
		else:
			ans = cnt * (cnt - 1)
			ans %= MOD
			ans *= fact[n - 2]
			ans %= MOD
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


import sys

def solve(n, a):
	s = a[0]
	min_ = min(a)
	k_min = a.count(min_)
	for i in range(1, n):
		s &= a[i]
	if min_ != s:
		return '0'
	fact = 1
	for i in range(2, n - 1):
		fact = fact * i % 1000000007
	return str(k_min * (k_min - 1) * fact % 1000000007)

def main():
	input = sys.stdin.readline
	print = sys.stdout.write
	res = ''
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		sub_res = solve(n, a)
		res += sub_res + '\n'
	print(res)
main()


import math

def binomialCoefficient(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res // (i + 1)
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = min(l)
	if l.count(x) <= 1:
		print(0)
		continue
	flag = 1
	for i in l:
		if i & x != x:
			flag = 0
			break
	if flag == 0:
		print(0)
		continue
	ends = l.count(x)
	ans = 2 * binomialCoefficient(ends, 2) * math.factorial(n - 2)
	print(ans % 1000000007)


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
getcontext().prec = 25
MOD = pow(10, 9) + 7
BUFSIZE = 8192
from collections import defaultdict

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
d = [1]
for i in range(1, 2 * pow(10, 5) + 1):
	d.append(d[-1] * i % MOD)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	f = l.count(l[0])
	odd = even = False
	for i in l:
		if i % 2:
			odd = True
		else:
			even = True
	if l[0] == 0:
		if f <= 1:
			print(0)
		else:
			ans = f * (f - 1) % MOD
			ans = ans * d[n - 2]
			print(ans % MOD)
	elif f < 2:
		print(0)
	else:
		an = True
		for i in range(n):
			if l[i] & l[0] != l[0]:
				an = False
				break
		if an:
			ans = f * (f - 1) % MOD
			ans = ans * d[n - 2]
			print(ans % MOD)
		else:
			print(0)


t = int(input())
for ti in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	min_val = min(arr)
	cnt = 0
	ans = -1
	for i in arr:
		if min_val & i != min_val:
			ans = 0
			break
		if i == min_val:
			cnt += 1
	if ans == 0 or cnt < 2:
		print(0)
		continue
	ans = cnt * (cnt - 1)
	mod = int(1000000000.0 + 7)
	for i in range(n - 2, 1, -1):
		ans = ans * i % mod
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a_vec = input().split(' ')
	a_vec = [int(a) for a in a_vec]
	m = min(a_vec)
	c = 0
	is_ok = True
	for i in range(n):
		if a_vec[i] == m:
			c += 1
		if a_vec[i] & m != m:
			is_ok = False
			break
	if is_ok == False:
		print(0)
	else:
		print(c * (c - 1) * math.factorial(n - 2) % (10 ** 9 + 7))


mod = 10 ** 9 + 7

def factorialMod(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i % mod
	return res
T = int(input())
for qq in range(T):
	n = int(input())
	a = [int(i) for i in input().split()]
	x = min(a)
	cnt = a.count(x)
	was = False
	for i in a:
		if i != x and i & x != x:
			was = True
			break
	if was:
		print(0)
		continue
	print(cnt * (cnt - 1) * factorialMod(n - 2) % mod)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj

def sieve():
	li = [True] * (10 ** 3 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	(prime, cur) = ([0] * 200, 0)
	for i in range(10 ** 3 + 5):
		if li[i] == True:
			prime[cur] = i
			cur += 1
	return prime

def SPF():
	mx = 10 ** 7 + 1
	spf = [mx] * mx
	spf[1] = 1
	for i in range(2, mx):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx, i):
				if i < spf[j]:
					spf[j] = i
	return spf

def prime(n, d):
	while n != 1:
		d[spf[n]] = d.get(spf[n], 0) + 1
		n = n // spf[n]
	return
mod = 1000000007
inf = 10000000000000000

def solve():
	n = II()
	a = L()
	a.sort()
	num = a[0]
	for i in range(2, n):
		num &= a[i]
	num2 = a[1]
	for i in range(2, n):
		num2 &= a[i]
	d = {}
	for ele in a:
		d[ele] = d.get(ele, 0) + 1
	ans = 0
	val = 1
	for i in range(2, n - 1):
		val *= i
		val %= mod
	if num == a[1] and num2 == a[0]:
		if a[0] == a[1]:
			ans += (d[a[1]] - 1) * val * d[a[1]]
			ans %= mod
		else:
			ans += d[a[0]] * val * d[a[1]] * 2
			ans %= mod
	print(ans)
	return
t = II()
for i in range(t):
	solve()


n = 2 * 10 ** 5 + 1
m = 10 ** 9 + 7
fac = [1]
for i in range(1, n):
	fac.append(fac[-1] * i % m)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr.count(arr[0]) == n:
		print(fac[n])
	else:
		out = arr[0]
		for i in arr:
			out &= i
		c = arr.count(out)
		if c > 1:
			print(c * (c - 1) * fac[n - 2] % m)
		else:
			print(0)


inf = int(1000000000.0 + 7)
for s in [*open(0)][2::2]:
	l = [*map(int, s.split())]
	n = len(l)
	a = max(l)
	for i in l:
		a &= i
	c = l.count(a)
	if c > 1:
		v = 1
		for i in range(2, n - 1):
			v = v * i % inf
		print(c * (c - 1) * v % inf)
	else:
		print(0)


import sys
import math
from collections import defaultdict as dd
from sys import stdin
input = stdin.readline
m = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	k = arr[0]
	for i in arr:
		k &= i
	if k == min(arr):
		c = arr.count(k)
		if c > 1:
			ans = c * (c - 1)
			for i in range(n - 2):
				ans = ans * (i + 1) % m
			print(ans)
		else:
			print(0)
	else:
		print(0)


mod = 10 ** 9 + 7
arr = [1]
val = 1
for i in range(1, 2 * 10 ** 5 + 1):
	val = val * i % mod
	arr.append(val)

def solve(n, A):
	minv = min(A)
	cnt = 0
	v1 = 0
	for i in range(n):
		if A[i] == minv:
			cnt = cnt + 1
		elif A[i] & minv != minv:
			v1 = 1
			break
	if cnt < 2 or v1 == 1:
		return 0
	else:
		fa = cnt * (cnt - 1) % mod
		fa = fa * arr[n - 2] % mod
		return fa
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	print(solve(n, A))


def solution():
	MOD = 7 + 10 ** 9
	n = int(input())
	arr = list(map(int, input().split()))
	mn = min(arr)
	cnt = 0
	same = True

	def fact(a):
		if a == 0:
			return 1
		else:
			ans = 1
			while a:
				ans = ans * a % MOD
				a -= 1
			return ans
	for i in range(n):
		if arr[i] == mn:
			cnt += 1
		if arr[i] != mn:
			same = False
		if arr[i] & mn != mn:
			print(0)
			return
	if same:
		print(int(fact(n) % (7 + 10 ** 9)))
		return
	if cnt < 2:
		print(0)
		return
	ans = cnt * (cnt - 1) * fact(n - 2) % (7 + 10 ** 9)
	print(int(ans))
	return
t = int(input())
while t:
	t -= 1
	solution()


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
tc = 1
mod = 10 ** 9 + 7
tc = int(input())
for test in range(1, tc + 1):
	n = int(input())
	a = list(map(int, input().split()))
	ans = a[0]
	for i in a:
		ans = i & ans
	c = a.count(ans)
	if c > 1:
		ans = c * (c - 1) % mod
		for i in range(1, n - 1):
			ans = ans * i % mod
	else:
		ans = 0
	print(ans)


import sys, functools, collections, bisect, math, heapq
input = sys.stdin.readline
mod = 10 ** 9 + 7
sys.setrecursionlimit(300000)

@functools.lru_cache(None)
def fac(n):
	ans = 1
	while n:
		ans *= n
		ans %= mod
		n -= 1
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	a.sort()
	d = collections.Counter(a)
	x = 10 ** 34 - 1
	for i in range(n):
		x = x & a[i]
	if d[x] <= 1:
		print(0)
		continue
	else:
		ans = d[x] * (d[x] - 1)
		ans %= mod
		ans *= fac(n - 2)
		ans %= mod
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(X) for X in input().split()]
	num = arr[0]
	for i in range(1, n):
		num &= arr[i]
	counter = 0
	for i in range(n):
		if arr[i] == num:
			counter += 1
	if counter >= 2:
		ans = counter * (counter - 1)
		ans %= mod
		for i in range(1, n - 2 + 1):
			ans *= i
			ans %= mod
		print(ans)
	else:
		print(0)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	an = a[0]
	for i in a:
		an &= i
	cnt = a.count(an)
	if cnt <= 1:
		print(0)
		continue
	ans = 2
	mod = 10 ** 9 + 7
	for i in range(1, n - 1):
		ans *= i
		ans %= mod
	ans *= ncr(cnt, 2, mod)
	ans %= mod
	print(ans)


for _ in range(int(input())):
	n = int(input())
	m = 10 ** 9 + 7
	a = list(map(int, input().split()))
	c = a[0]
	for i in range(1, n):
		c = c & a[i]
	crr = 0
	for i in range(n):
		if a[i] == c:
			crr += 1
	if crr >= 2:
		f = 1
		for i in range(1, n - 1):
			f *= i
			f = f % m
		f = f * (crr % m) * ((crr - 1) % m) % m
		f = f % m
		print(f)
	else:
		print(0)


mod = 10 ** 9 + 7

def main():
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	for i in range(n):
		x &= a[i]
	for i in range(n):
		a[i] -= x
	x = 0
	for i in range(n):
		if a[i] == 0:
			x += 1
	s = x * (x - 1) % mod
	if x < 2:
		print(0)
	else:
		x = 1
		for i in range(1, n - 1):
			x *= i
			x %= mod
		print(s * x % mod)
t = int(input())
for i in range(t):
	main()