Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
A = [ [int(j) for j in input().split()] for i in range(N)]
# I = [ [j for j in range(1,N+1) if Ai[j]] for Ai in A]
MOD = 10**9 + 7

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

# import numpy as np
# npA = np.array(A).astype(np.uint64)

def prod2(A,B):
    global N
    C = [ [0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            tmp = 0
            for k in range(N):
                tmp += A[i][k] * B[k][j]
            C[i][j] = tmp
    return C


def prod(A,n):
    if n == 0:
        C = [ [0]*(N) for _ in range(N)]
        for i in range(N):
                C[i][i] = 1
        return C

    elif n == 1:
        return A

    elif n % 2 == 1:
        return prod2( A , prod(A,n-1))

    elif n % 2 == 0:
        return prod2( prod(A,n//2), prod(A,n//2))
    
    

ans = 0
# M = np.linalg.matrix_power(A, K)
M = prod(A,K)
for i in range(1,N+1):
    for j in range(1,N+1):
        # dp[i][j] = np.linalg.matrix_power(A, K)[i-1][j-1].item()
        # ans += dp[i][j] % MOD
        # ans += np.linalg.matrix_power(A, K)[i-1][j-1].item() % MOD
        # ans += M[i-1,j-1].item() % MOD
        ans += M[i-1][j-1] % MOD

print(ans%MOD)



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
A = [ [int(j) for j in input().split()] for i in range(N)]
# I = [ [j for j in range(1,N+1) if Ai[j]] for Ai in A]
MOD = 10**9 + 7

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

S = [[1]*N]
KDIG = [0]*70

for i in range(1,61):
    if K % (1<<i) :
        KDIG[i-1] = 1
        K -= K%(1<<i)


def prod(A,B,k,l,m):#A:k*l,B:l*m
 
    C=[[None for i in range(m)] for j in range(k)]
 
    for i in range(k):
        for j in range(m):
            ANS=0
            for pl in range(l):
                ANS=(ANS+A[i][pl]*B[pl][j])%MOD
 
            C[i][j]=ANS
 
    return C



for i in range(60):
    if KDIG[i] == 1:
        S = prod(S,A,1,N,N)

    A = prod(A,A,N,N,N)
print(sum(S[0])%MOD)

# for i in range(1,N+1):
#     for j in range(1,N+1):
#         # dp[i][j] = np.linalg.matrix_power(A, K)[i-1][j-1].item()
#         # ans += dp[i][j] % MOD
#         # ans += np.linalg.matrix_power(A, K)[i-1][j-1].item() % MOD
#         # ans += M[i-1,j-1].item() % MOD
#         ans += M[i-1][j-1] % MOD

# print(ans%MOD)

Buggy python submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
A = [ [int(j) for j in input().split()] for i in range(N)]
# I = [ [j for j in range(1,N+1) if Ai[j]] for Ai in A]
MOD = 10**9 + 7

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

import numpy as np
npA = np.array(A).astype(np.uint64)

def prod2(A,B):
    global N
    C = [ [0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            tmp = 0
            for k in range(N):
                tmp += A[i][k] * B[k][j]
            C[i][j] = tmp
    return C


def prod(A,n):
    if n == 0:
        C = [ [0]*(N) for _ in range(N)]
        for i in range(N):
                C[i][i] = 1
        return C

    elif n == 1:
        return A

    elif n % 2 == 1:
        return prod2( A , prod(A,n-1))

    elif n % 2 == 0:
        return prod2( prod(A,n//2), prod(A,n//2))
    
    

ans = 0
# M = np.linalg.matrix_power(A, K)
M = prod(A,K)
for i in range(1,N+1):
    for j in range(1,N+1):
        # dp[i][j] = np.linalg.matrix_power(A, K)[i-1][j-1].item()
        # ans += dp[i][j] % MOD
        # ans += np.linalg.matrix_power(A, K)[i-1][j-1].item() % MOD
        # ans += M[i-1,j-1].item() % MOD
        ans += M[i-1][j-1] % MOD

print(ans%MOD)



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
A = [ [int(j) for j in input().split()] for i in range(N)]
# I = [ [j for j in range(1,N+1) if Ai[j]] for Ai in A]
MOD = 10**9 + 7

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

S = [[1]*N]
KDIG = [0]*70

for i in range(1,61):
    if K % (1<<i) :
        KDIG[i-1] = 1
        K -= K%(1<<i)


def prod(A,B,k,l,m):#A:k*l,B:l*m
 
    C=[[None for i in range(m)] for j in range(k)]
 
    for i in range(k):
        for j in range(m):
            ANS=0
            for pl in range(l):
                ANS=(ANS+A[i][pl]*B[pl][j])%MOD
 
            C[i][j]=ANS
 
    return C



for i in range(60):
    if KDIG[i] == 1:
        S = prod(S,A,1,N,N)

    A = prod(A,A,N,N,N)
print(sum(S[0])%MOD)

# for i in range(1,N+1):
#     for j in range(1,N+1):
#         # dp[i][j] = np.linalg.matrix_power(A, K)[i-1][j-1].item()
#         # ans += dp[i][j] % MOD
#         # ans += np.linalg.matrix_power(A, K)[i-1][j-1].item() % MOD
#         # ans += M[i-1,j-1].item() % MOD
#         ans += M[i-1][j-1] % MOD

# print(ans%MOD)