Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())

A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [-1] * (1 << N)
dp[(1 << N) - 1] = 1

def dfs(group, count):
    if dp[group] != -1:
        return dp[group]
    res = 0
    for i in range(N):
        if (group >> i) & 1 == 0 and A[count][i] == 1:
            res += dfs(group + (1 << i), count + 1)
    dp[group] = res % MOD
    return dp[group]

tmp = dfs(0, 0)

print (tmp)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())

A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [-1] * (1 << N)
dp[(1 << N) - 1] = 1

def dfs(group, count):
    if dp[group] != -1:
        return dp[group]
    res = 0
    for i in range(N):
        if (group >> i) & 1 == 0 and A[count][i] == 1:
            res += dfs(group + (1 << i), count + 1)
    dp[group] = res % MOD
    return dp[group]

tmp = dfs(0, 0)

print (tmp)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

MOD = 10 ** 9 + 7

N = int(input())
A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [[0] * (1 << (N)) for _ in range(N + 1)]

dp[0][0] = 1
# for tmp in dp:
#     print (tmp)
lst = [0]

for i in range(N):
    tmp = []
    for j in range(N):
        if A[i][j] == 0: #仲良くない時
            continue
        #以下仲が良い時のみの処理        
        for k in lst:
            if (1 << j) & k == 0: #まだペアになっていない女の人の時
                dp[i + 1][k + (1 << j)] = (dp[i + 1][k + (1 << j)] + dp[i][k]) % MOD
                tmp.append(k + (1 << j))
    
        # print (dp[i + 1])
        # print (lst)
    lst = tmp

print (dp[N][(1 << N) - 1])

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())

A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [-1] * (1 << N)
dp[(1 << N) - 1] = 1

def dfs(group, count):
    if dp[group] != -1:
        return dp[group]
    res = 0
    for i in range(N):
        if (group >> i) & 1 == 0 and A[count][i] == 1:
            res += dfs(group + (1 << i), count + 1)
    dp[group] = res % MOD
    return dp[group]

tmp = dfs(0, 0)

print (tmp)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

# import time
# s = time.time()

MOD = 10 ** 9 + 7

N = int(input())
A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [[0] * (1 << (N)) for _ in range(N + 1)]

dp[0][0] = 1
# for tmp in dp:
#     print (tmp)

for i in range(N):
    for j in range(N):
        if A[i][j] == 0: #仲良くない時
            continue
        #以下仲が良い時のみの処理
        for k in range(1 << N):
            if (1 << j) & k == 0: #まだペアになっていない女の人の時
                dp[i + 1][k + (1 << j)] = (dp[i + 1][k + (1 << j)] + dp[i][k]) % MOD
        # print (dp[i + 1])

print (dp[N][(1 << N) - 1])

# f = time.time()

# print (f - s, '秒')

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())

A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [-1] * (1 << N)
dp[(1 << N) - 1] = 1

def dfs(group, count):
    if dp[group] != -1:
        return dp[group]
    res = 0
    for i in range(N):
        if (group >> i) & 1 == 0 and A[count][i] == 1:
            res += dfs(group + (1 << i), count + 1)
    dp[group] = res % MOD
    return dp[group]

tmp = dfs(0, 0)

print (tmp)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

MOD = 10 ** 9 + 7

N = int(input())
A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [[0] * (1 << (N)) for _ in range(N + 1)]

dp[0][0] = 1
# for tmp in dp:
#     print (tmp)

for i in range(N):
    for j in range(N):
        if A[i][j] == 0: #仲良くない時
            continue
        #以下仲が良い時のみの処理
        for k in range(1 << N):
            if (1 << j) & k == 0: #まだペアになっていない女の人の時
                dp[i + 1][k + (1 << j)] = (dp[i + 1][k + (1 << j)] + dp[i][k]) % MOD
        # print (dp[i + 1])

print (dp[N][(1 << N) - 1])    

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())

A = [tuple(map(int, input().split())) for _ in range(N)]

dp = [-1] * (1 << N)
dp[(1 << N) - 1] = 1

def dfs(group, count):
    if dp[group] != -1:
        return dp[group]
    res = 0
    for i in range(N):
        if (group >> i) & 1 == 0 and A[count][i] == 1:
            res += dfs(group + (1 << i), count + 1)
    dp[group] = res % MOD
    return dp[group]

tmp = dfs(0, 0)

print (tmp)