https://codeforces.com/contest/1513/problem/B
思路:不是很会证明,但能直观感受到是最小的相同数才能满足。结合打表猜测。
贴cf的官方证明:
用前缀的方法来证明,其开始要放的数和结尾要放的数一定一样的。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
LL fac[maxn];
map<LL,LL>map1;
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
fac[0]=1;
for(LL i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;
LL t;cin>>t;
while(t--){
LL n;cin>>n;
map1.clear();
for(LL i=1;i<=n;i++) cin>>a[i],map1[a[i]]++;
sort(a+1,a+1+n);
LL temp1=a[2];
for(LL i=2;i<=n;i++){
temp1&=a[i];
}
if(temp1!=a[1]){
cout<<"0"<<"\n";
continue;
}
LL ans=0;
for(auto i:map1){
if(i.first==a[1]&&i.second>=2){
///cout<<"i.second="<<i.second<<"\n";
ans=(ans%mod+( ( (i.second%mod*(i.second-1)%mod)%mod)*fac[n-2]%mod)%mod)%mod;
}
}
cout<<ans<<"\n";
}
return 0;
}