Buggy python submission (verdict: Time Limit Exceeded):
import sys
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
a = [LI() for _ in range(N)]
dp = [[0] * (2**N) for _ in range(N)]

q = [[] for _ in range(N)]
b = 1
for i in range(N):
    if a[0][i]:
        dp[0][b] = 1
        q[0].append(b)
    b <<= 1

for i in range(1,N):
    for k in q[i-1]:
        v = dp[i-1][k]
        b = 1
        for l in a[i]:
            if l and k & b == 0:
                m = k | b
                if dp[i][m] == 0: q[i].append(m)
                dp[i][m] = (dp[i][m] + v) % MOD
            b <<= 1

print(dp[-1][2**N-1])

Fixed submission:
import sys

def main():
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    N = NI()
    a = [LI() for _ in range(N)]
    dp = [[0] * (2**N) for _ in range(N)]

    q = [[] for _ in range(N)]
    b = 1
    for i in range(N):
        if a[0][i]:
            dp[0][b] = 1
            q[0].append(b)
        b <<= 1

    for i in range(1,N):
        for k in q[i-1]:
            v = dp[i-1][k]
            b = 1
            for l in a[i]:
                if l and k & b == 0:
                    m = k | b
                    if dp[i][m] == 0: q[i].append(m)
                    dp[i][m] = (dp[i][m] + v) % MOD
                b <<= 1

    print(dp[-1][2**N-1])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys,queue,math,copy,itertools,bisect,collections,heapq
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
a = [LI() for _ in range(N)]
dp = [[0] * (2**N) for _ in range(N)]

q = [set() for _ in range(N)]
b = 1
for i in range(N):
    if a[0][i]:
        dp[0][b] = 1
        q[0].add(b)
    b <<= 1


for i in range(1,N):
    for k in q[i-1]:
        v = dp[i-1][k]
        b = 1
        for l in a[i]:
            if l and k & b == 0:
                m = k | b
                dp[i][m] = (dp[i][m] + v) % MOD
                q[i].add(m)
            b <<= 1

ans = dp[-1][2**N-1] % MOD
print(ans)

Fixed submission:
import sys

def main():
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    N = NI()
    a = [LI() for _ in range(N)]
    dp = [[0] * (2**N) for _ in range(N)]

    q = [[] for _ in range(N)]
    b = 1
    for i in range(N):
        if a[0][i]:
            dp[0][b] = 1
            q[0].append(b)
        b <<= 1

    for i in range(1,N):
        for k in q[i-1]:
            v = dp[i-1][k]
            b = 1
            for l in a[i]:
                if l and k & b == 0:
                    m = k | b
                    if dp[i][m] == 0: q[i].append(m)
                    dp[i][m] = (dp[i][m] + v) % MOD
                b <<= 1

    print(dp[-1][2**N-1])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys,queue,math,copy,itertools,bisect,collections,heapq
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
a = [LI() for _ in range(N)]
dp = [{} for _ in range(N)]

b = 1
for i in range(N):
    if a[0][i]: dp[0][b] = 1
    b <<= 1

for i in range(1,N):
    for k,v in dp[i-1].items():
        b = 1
        for l in a[i]:
            if l and k & b == 0:
                m = k | b
                if m in dp[i]: dp[i][m] = (dp[i][m] + v) % MOD
                else: dp[i][m] = v
            b <<= 1
ans = sum(dp[-1].values()) % MOD
print(ans)

Fixed submission:
import sys

def main():
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    N = NI()
    a = [LI() for _ in range(N)]
    dp = [[0] * (2**N) for _ in range(N)]

    q = [[] for _ in range(N)]
    b = 1
    for i in range(N):
        if a[0][i]:
            dp[0][b] = 1
            q[0].append(b)
        b <<= 1

    for i in range(1,N):
        for k in q[i-1]:
            v = dp[i-1][k]
            b = 1
            for l in a[i]:
                if l and k & b == 0:
                    m = k | b
                    if dp[i][m] == 0: q[i].append(m)
                    dp[i][m] = (dp[i][m] + v) % MOD
                b <<= 1

    print(dp[-1][2**N-1])

if __name__ == '__main__':
    main()