AND Sequences
Contest: Divide by Zero 2021 and Codeforces Round 714 (Div. 2)
Difficulty rating: 1400
Tags: bitmasks, combinatorics, constructive algorithms, math

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 format:
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 format:
Output $$$t$$$ lines, where the $$$i$$$-th line contains the number of good permutations in the $$$i$$$-th test case modulo $$$10^9 + 7$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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$$$.

Example input:
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1

Example output:
6
0
36
4

Editorial:
Consider an arbitrary sequence $$$b_1,b_2,\dots,b_n$$$. First let us define the arrays $$$AND\_pref$$$ and $$$AND\_suf$$$ of length $$$n$$$ where $$$AND\_pref_i = b_1 \: \& \: b_2 \: \& \: \dots \: \& \: b_i$$$ and $$$AND\_suf_i = b_{i} \: \& \: b_{i+1} \: \& \: \dots \: \& \: b_n$$$ .
According to the definition of good sequence:
$$$AND\_pref_1 = AND\_suf_2$$$ which means $$$b_1 = b_2 \: \& \: b_3 \: \& \: \dots \: \& \: b_n$$$ .
Now $$$AND\_pref_2 \leq AND\_pref_1 = AND\_suf_2 \leq AND\_suf_3$$$. Also according to definition of good sequence, $$$AND\_pref_2 = AND\_suf_3$$$. This means that $$$b_1 = AND\_pref_2 = AND\_suf_3$$$. Similarly, for all $$$i$$$ from $$$1$$$ to $$$n$$$, we get $$$AND\_pref_i = b_1$$$ and $$$AND\_suf_i = b_1$$$.
Therefore for the sequence to be good, $$$b_1 = b_n$$$ and the $$$b_i$$$ must be a super mask of $$$b_1$$$ for all $$$i$$$ from $$$2$$$ to $$$n-1$$$.
Initially, we have an array $$$a_1,a_2,\dots,a_n$$$. Let the minimum value among these elements be $$$x$$$. Let the number of elements that have the value of $$$x$$$ be $$$cnt$$$.
In order to rearrange the elements of $$$a_1,a_2,\dots,a_n$$$ to a good sequence, we need to have $$$cnt \geq 2$$$ and the remaining elements need to be a super mask of $$$x$$$. If we don't meet this criterion, then the answer is $$$0$$$. Else the answer will be $$$(cnt \cdot (cnt-1) \cdot (n-2)!) \% (10^9+7)$$$.
The time complexity is $$$O(n)$$$.