Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned long long
- #define ld long double
- #define vi vector<int>
- #define vll vector<long long>
- #define pb push_back
- #define p_b pop_back
- #define mod (ll)1000000007
- using namespace std;
- string bin(ll x){
- string t;
- while(x > 0){
- t = to_string(x%2)+t;
- x /= 2;
- }
- int l = t.length();
- for(int i = 0; i < 31-l; i++){
- t = "0"+t;
- }
- return t;
- }
- ll fact(int n, int r){
- ll k = 1;
- for(int i = n-r+1; i <= n; i++){
- k = ((k%mod)*(i)%mod)%mod;
- }
- return k;
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int t;
- cin >> t;
- while(t--){
- int n;
- cin >> n;
- vector<string> v;
- for(int i = 0; i < n; i++){
- ll x;
- cin >> x;
- v.pb(bin(x));
- }
- int count = 0;
- bool dp[n][31] = {0};
- for(int i = 0; i < n; i++){
- if(v[i][0] == '0'){
- dp[i][0] = 1;
- count++;
- }
- }
- if(count == 1){
- cout << "0\n";
- } else{
- bool f = true;
- if(count == 0){
- for(int i = 0; i < n; i++){
- dp[i][0] = 1;
- }
- }
- for(int j = 1; j < 31; j++){
- int count1 = 0;
- for(int i = 0; i < n; i++){
- if(v[i][j] == '0' && dp[i][j-1] == 1){
- count1++;
- dp[i][j] = 1;
- }
- }
- if(count1 == 0){
- for(int i = 0; i < n; i++){
- dp[i][j] = dp[i][j-1];
- if(dp[i][j-1] == 1){
- count1++;
- }
- }
- }
- if(count1 != 1){
- count = count1;
- } else{
- f = false;
- break;
- }
- }
- if(f){
- if(count == 0){
- cout << fact(n, n)%mod << "\n";
- } else{
- cout << ((fact(n-2, n-count)%mod)*(fact(count, count)%mod))%mod << "\n";
- }
- } else{
- cout << "0\n";
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement