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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;

#define LEN(x) (int)x.size()
#define ALL(x) x.begin(), x.end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
using vpii = vector<pii>;

template<typename T, typename M = __gnu_pbds::null_type>
using ordered_set = __gnu_pbds::tree<T, M, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

inline int fcmp(double x, double y)
{ return (x < y + EPS ? (x < y - EPS ? -1 : 0) : 1);}

template<const ll mod>
struct modint
{
    ll val;
    
    inline ll normalize(ll n)
    {
        if(n >= mod || n < -mod)
            n %= mod;
        if(n < 0)
            n += mod;
        return n;
    }

    modint(){ val = 0;}
    template<class T> modint(T x = 0) { val = normalize(x);}
    template<class T> explicit operator T() { return static_cast<T>(val);}

    modint<mod>& operator += (const modint<mod> o) { return *this = modint<mod>(val + o.val);}   
    template<class T> modint<mod>& operator += (const T o) { return *this = modint<mod>(val + normalize(o));}   
    modint<mod>& operator -= (const modint<mod> o) { return *this = modint<mod>(val - o.val);}   
    template<class T> modint<mod>& operator -= (const T o) { return *this = modint<mod>(val - normalize(o));}   
    modint<mod>& operator *= (const modint<mod> o) { return *this = modint<mod>(val * o.val);}   
    template<class T> modint<mod>& operator *= (const T o) { return *this = modint<mod>(val * normalize(o));}

    modint<mod>& operator++() { return *this += 1;}
    modint<mod>& operator--() { return *this -= 1;}

    modint<mod> operator + (const modint<mod> o) { return modint<mod>(val + o.val);}   
    template<class T> modint<mod> operator + (const T o) { return modint<mod>(val + o);}   
    modint<mod> operator - (const modint<mod> o) { return modint<mod>(val - o.val);}   
    template<class T> modint<mod> operator - (const T o) { return modint<mod>(val - o);}   
    modint<mod> operator * (const modint<mod> o) { return modint<mod>(val * o.val);}   
    template<class T> modint<mod> operator * (const T o) { return modint<mod>(val * o);}

    bool operator == (const modint<mod> o) { return val == o.val;}
    template<class T> bool operator == (const T o) { return val == normalize(o);}
    bool operator != (const modint<mod> o) { return val != o.val; }
    template<class T> bool operator != (const T o) { return val != normalize(o);}

    bool operator < (const modint<mod> o) { return val < o.val;}
    template<class T> bool operator < (const T o) { return val < normalize(o);}

    modint<mod>& operator /= (const modint<mod> o) { return *this *= o.inv();}
    template<class T> modint<mod>& operator /= (const T o) { return *this *= modint<mod>(o).inv();}
    modint<mod> operator / (modint<mod> o) {modint<mod> ans(val); return ans * o.inv();}
    template<class T> modint<mod> operator / (T o) { return modint<mod>(val) / modint<mod>(o);}

    modint<mod> inv() { return fexp(*this, mod - 2);}

    friend istream& operator >> (istream& in, modint<mod>& n)
    {
        ll x; in >> x;
        n = modint<mod>(x);
        return in;
    }

    friend ostream& operator << (ostream& out, modint<mod>& n)
    {
        return out << n.val;
    }
};

const int N = 22, MSK = 1 << N;
modint<MOD> dp[MSK];
bool a[N][N];
int n;

int main()
{
    ios_base::sync_with_stdio(false);
    cout.precision(10);
    
    cin >> n;

    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> a[i][j];

    int full = (1 << n) - 1;

    dp[0] = 1;
    for(int msk = 0; msk <= full; msk++)
    {
        int i = __builtin_popcount(msk);
        for(int j = 0; j < n; j++)
            if(a[i][j] && !(msk & (1 << j)))
                dp[msk | (1 << j)] += dp[msk];
    }

    cout << dp[full] << endl;
    
    return 0;
}