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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public static class BitArrayUtils
    {
        public static BitArray ToBitArray(this int l)
        {
            return new BitArray(BitConverter.GetBytes(l));
        }
        public static int ToInt32(this BitArray bitArray)
        {
            var array = new byte[4];
            bitArray.CopyTo(array, 0);
            return BitConverter.ToInt32(array, 0);
        }
    }
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var a = consoleReader.Repeat(N).SplitInt.ToArray();

            var list = new HashSet<int>(a[0].Select((v, i) => new { v, x = 1 << i }).Where(o => o.v == 1).Select(o => o.x));
            var dp = new Mod[N, 1 << N];
            foreach (var item in list)
            {
                dp[0, item] = 1;
            }

            for (int n = 1; n < N; n++)
            {
                var nextList = new HashSet<int>();
                foreach (var b in list)
                {
                    var bits = b.ToBitArray();
                    for (int i = 0; i < N; i++)
                    {
                        var pair = a[n][i];
                        if (pair == 1 && !bits[i])
                        {
                            var newB = b | (1 << i);
                            nextList.Add(newB);
                            dp[n, newB] += dp[n - 1, b];
                        }
                    }
                }
                list = nextList;
            }

            return dp[N - 1, (1 << N) - 1];
        }
        struct Mod : IEquatable<Mod>
        {
            public const long mod = 1000000007;
            public readonly long val;
            public Mod(long val) { this.val = val; }
            public override bool Equals(object obj) => (obj is Mod) ? this == ((Mod)obj) : false;
            public bool Equals(Mod obj) => this == obj;
            public override int GetHashCode() => val.GetHashCode();
            public override string ToString() => val.ToString();
            public static implicit operator Mod(long x) => new Mod(x);
            public static Mod operator +(Mod x, Mod y) => (x.val + y.val) % mod;
            public static Mod operator -(Mod x, Mod y) => x.val >= y.val ? (x.val - y.val) % mod : (x.val - y.val) % mod + mod;
            public static Mod operator *(Mod x, Mod y) => (x.val * y.val) % mod;
            public static Mod operator /(Mod x, Mod y) => x * y.Inverse();
            public static bool operator ==(Mod x, Mod y) => x.val == y.val;
            public static bool operator !=(Mod x, Mod y) => x.val != y.val;
            public Mod Inverse()
            {
                long a = val, b = mod, u = 1, v = 0;
                while (b > 0)
                {
                    long t = a / b;
                    var b2 = a - t * b;
                    a = b; b = b2;
                    var v2 = u - t * v;
                    u = v; v = v2;
                }
                u %= mod;
                if (u < 0) u += mod;
                return u;
            }
            public static Mod Pow(Mod x, int y)
            {
                Mod res = 1;
                for (; y > 0; y >>= 1)
                {
                    if ((y & 1) == 1) res *= x;
                    x *= x;
                }
                return res;
            }

            public static Factor CreateFactor(int max) => new Factor(max);
            public class Factor
            {
                private readonly Mod[] fac, finv;
                public Factor(int max)
                {
                    var inv = new Mod[max];
                    fac = new Mod[max]; finv = new Mod[max];
                    fac[0] = fac[1] = 1;
                    finv[0] = finv[1] = 1;
                    inv[1] = 1;
                    for (int i = 2; i < max; i++)
                    {
                        fac[i] = fac[i - 1] * i;
                        inv[i] = mod - inv[mod % i].val * (mod / i) % mod;
                        finv[i] = finv[i - 1] * inv[i];
                    }
                }

                // 二項係数計算
                public Mod Combine(int n, int k)
                {
                    if (n < k) return 0;
                    if (n < 0 || k < 0) return 0;
                    return fac[n] * finv[k] * finv[n - k];
                }

                public Mod Factorial(int n) => fac[n];
                public Mod FactorialInvers(int n) => finv[n];
            }
        }

        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> SplitString { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}