from numpy import*

N,K,*A=list(map(int,open(0).read().split()))

X=matrix(A,"O").reshape(N,N)

M=10**9+7

S=1

while K:S*=~K%2or X;K//=2;X=X*X%M

print((sum(S)%M))
import numpy as np

N, K = list(map(int, input().split()))

M = 10**9+7

xs = []

for _ in range(N):

    xs.append(list(map(int, input().split())))

X = np.array(xs, dtype="object")

S = np.ones(N, dtype="object")

#Xs = [X]

for _ in range(60):

#    Xs.append(np.dot(Xs[-1],Xs[-1])%M)

    if K%2:

        S = np.dot(X, S)%M

    K = K//2

    X = np.dot(X, X)%M

S = np.array(S, dtype="int64")

r = 0

for s in S:

    r = (r + s)%M

print(r)
from numpy import *

N, K, *A = list(map(int, open(0).read().split()))
X = matrix(A, "O").reshape(N, N)
M = 10**9 + 7
S = 1
while K:
    S *= ~K % 2 or X
    K //= 2
    X = X * X % M
print((sum(S) % M))

import numpy as np

N, K = list(map(int, input().split()))
M = 10**9 + 7
xs = []
for _ in range(N):
    xs.append(list(map(int, input().split())))
X = np.array(xs, dtype="object")
S = np.ones(N, dtype="object")
# Xs = [X]
for _ in range(60):
    #    Xs.append(np.dot(Xs[-1],Xs[-1])%M)
    if K % 2:
        S = np.dot(X, S) % M
    K = K // 2
    X = np.dot(X, X) % M
S = np.array(S, dtype="int64")
r = 0
for s in S:
    r = (r + s) % M
print(r)

-from numpy import * +import numpy as np -N, K, *A = list(map(int, open(0).read().split())) -X = matrix(A, "O").reshape(N, N) +N, K = list(map(int, input().split())) -S = 1 -while K: -    S *= ~K % 2 or X -    K //= 2 -    X = X * X % M -print((sum(S) % M)) +xs = [] +for _ in range(N): +    xs.append(list(map(int, input().split()))) +X = np.array(xs, dtype="object") +S = np.ones(N, dtype="object") +# Xs = [X] +for _ in range(60): +    #    Xs.append(np.dot(Xs[-1],Xs[-1])%M) +    if K % 2: +        S = np.dot(X, S) % M +    K = K // 2 +    X = np.dot(X, X) % M +S = np.array(S, dtype="int64") +r = 0 +for s in S: +    r = (r + s) % M +print(r)