Problem:
Score : 100 points

Problem Statement

There are N men and N women, both numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}.
If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not.

Taro is trying to make N pairs, each consisting of a man and a woman who are compatible.
Here, each man and each woman must belong to exactly one pair.

Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 21

a_{i, j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Sample Input 1

3
0 1 1
1 0 1
1 1 1

Sample Output 1

3

There are three ways to make pairs, as follows ((i, j) denotes a pair of Man i and Woman j):

(1, 2), (2, 1), (3, 3)

(1, 2), (2, 3), (3, 1)

(1, 3), (2, 1), (3, 2)

Sample Input 2

4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

Sample Output 2

1

There is one way to make pairs, as follows:

(1, 2), (2, 4), (3, 1), (4, 3)

Sample Input 3

1
0

Sample Output 3

0

Sample Input 4

21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

Sample Output 4

102515160

Be sure to print the number modulo 10^9 + 7.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

	#include<bits/stdc++.h>
	using namespace std;
	typedef long long ll;
	#define f(i,n) for(ll i=0;i<n;i++)
	#define pb push_back
	int mod=1e9+7;
	int gcd (int a, int b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
	ll fast(ll a,ll n){
		a%=mod;
		ll res=1;
		while(n>0){
			if(n&1)res=res*a%mod;
			a=a*a%mod;
			n>>=1;
		}
		return res;
	}
	int func(int a){
		int sum=0;
		while(a){
			sum+=a%10;
			a/=10;
		}
		return sum;
	}
	// vector<int> v;
const int maxn=22;

  int dp[(1<<21)][22];
  // int res=0;
  // int mod=1e9+7;
  int dfs(vector<vector<int>>&a,long long vis,long long j ){
    int n=a.size();
      if(j==n){return 1;}
      // long long res=0;
      if(dp[vis][j]!=-1)return dp[vis][j]%mod;
      dp[vis][j]=0;
      for(int i=0;i<n;i++){
        if(!(vis&(1<<i))&&(a[i][j]==1)){
          // cout<<"asas"<<i<<" asgva"<<vis<<endl;
          long long temp=vis|(1<<i);
          dp[vis][j]+=dfs(a,temp,j+1)%mod;
          dp[vis][j]%=mod;

        }

        // if(!vis[i]&&a[i][j]==1){
        //   vis[i]=true;
        //   dfs(a,vis,j+1);
        //   vis[i]=false;
        // }
      }
      return dp[vis][j]%mod;


  }
	int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);
    memset(dp,-1,sizeof dp);
    int n;
    cin>>n;
    vector<vector<int>>a(n,vector<int>(n));
    f(i,n){
      f(j,n) cin>>a[i][j];
    }
    // vector<bool>vis(n,false);
    // long long vis=1;
    long long res=0;
  for(int i=0;i<n;i++){
    if(a[i][0]==1){
      // cout<<i<<endl;
      res+=dfs(a,1<<i,1);}
    res%=mod;
  }
   cout<<res;
		return 0;
	}