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: Memory Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
# define ll long long int
# define M 1000000007
ll n;
ll dp[30][8388606];
ll solve(vector<vector<ll>>&v,ll i,ll mask,ll jod)
{
  if(i>n)
  {
    if(mask==jod)
    {
      return 1;
    }
    else{
      return 0;
    }
  }

  if(dp[i][mask]!=-1)
  {
    return dp[i][mask];
//    cout<<"Here"<<endl;
  }
  //cout<<"mask "<<mask<<endl;
  ll final=0;
  //cout<<"final "<<final<<endl;
  for(int j=0;j<v[i].size();j++)
  {
    ll r= v[i][j];
    if(((1<<r)&mask)==0)
    {
      final=(final + solve(v,i+1,(mask|(1<<r)),jod))%M;
      dp[i][mask]=final;
    }

  }

  return dp[i][mask]=final;
}

int main()
{

  cin>>n;
  //int mat[n][n];
  memset(dp,-1,sizeof(dp));
  vector<vector<ll>>v(n+1);
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      ll a;
      cin>>a;
      if(a==1)
      { //cout<<"deal "<<endl;
        v[i+1].push_back(j+1);
        //cout<<" element inserted "<<j+1<<endl;
      }
    }
  }

  // for(int i=1;i<=n;i++)
  // {
  //   cout<<i<<" : ";
  //   for(int j=0;j<v[i].size();j++)
  //   {
  //     cout<<v[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }
//
// cout<<" output "<<endl;
//   cout<<v[1][0]<<endl;
//   cout<<v[1][1]<<endl;
//   cout<<v[1][2]<<endl;
//   cout<<v[1][3]<<endl;
// ll total=2*(pow(2,22)-1);
// cout<<"Total "<<total<<endl;
  ll jod=2*(pow(2,n)-1);
  // cout<<"jod "<<jod<<endl;
  cout<<solve(v,1,0,jod)<<endl;
}