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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AtCoder
{
	public class ABC
	{
		static readonly int mod = 1000000007;  // 10^9+7

		static void Main(string[] args)
		{
			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });


			//string s = Console.ReadLine();
			int n = int.Parse(Console.ReadLine());
			//long x = long.Parse(Console.ReadLine());
			//int[] vs = Console.ReadLine().Split().Select(int.Parse).ToArray();
			//long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray();
			int[][] a = new int[n][];
			for (int i = 0; i < n; i++) {
				a[i] = Console.ReadLine().Split().Select(int.Parse).ToArray();
			}
#if true
			long[,] dp = new long[n + 1, 1 << n];
			dp[0, 0] = 1;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < dp.GetLength(1); j++) {
					if (i == PopCount(j)) {
						//System.Diagnostics.Debug.Write($"dp[{i},{j,2}({Convert.ToString(j, 2).PadLeft(n, '0')})]{dp[i, j]} -> ");
						for (int k = 0; k < n; k++) {
							int msk = 1 << k;
							if (a[i][k] == 1 && (j & msk) == 0) {
								dp[i + 1, j | msk] += dp[i, j];
							}
							//System.Diagnostics.Debug.Write($"dp[{i + 1},{j | msk,2}({Convert.ToString(j | msk, 2).PadLeft(n, '0')})]{dp[i + 1, j | msk]} ");
						}
						//System.Diagnostics.Debug.WriteLine("");
					}
				}
				//System.Diagnostics.Debug.WriteLine("");
			}
			//System.Diagnostics.Debug.WriteLine($"dp[{n},{(1 << n) - 1,2}({Convert.ToString((1 << n) - 1, 2).PadLeft(n, '0')})] = {dp[n, (1 << n) - 1]}");

#else
			long ans = 0;
			bool[] xf = new bool[n];
			bool[] yf = new bool[n];
			for (int i = 0; i < n; i++) {
				ans += saiki(a, i, 0, xf, yf, "");
			}
			Console.WriteLine(ans);
#endif


			Console.Out.Flush();
		}

		static int PopCount(long x)
		{
			int ret = 0;
			while (x != 0) {
				x &= x - 1;
				ret++;
			}
			return ret;
		}

		static long saiki(int[][] a, int x, int y, bool[] xf, bool[] yf, string s)
		{
			long result = 0;
			if (a[x][y] == 0 || xf[x] || yf[y]) {
				return 0;
			}
			xf[x] = true;
			yf[y] = true;
			s += $"a[{x},{y}]:{a[x][y]} ";
			if (y == a.Length - 1) {
				result = 1;
				System.Diagnostics.Debug.WriteLine(s);
			} else {
				for (int i = 0; i < a.Length; i++) {
					result += saiki(a, i, y + 1, xf, yf, s);
				}
			}
			xf[x] = false;
			yf[y] = false;
			return result;
		}

	}
}