# dp[n][i][j]=(頂点iから頂点jへ行く長さnのパスの個数)

# dp[n][i][j]=sum(dp[n-1][i][k]*a[k][j])⇒DP[n]=DP[n-1]*A DP[0]=Eより DP[n]=A^n

n,k=list(map(int,input().split()))

a=[list(map(int,input().split())) for i in range(n)]

dp=[[0]*n for i in range(n)]

mod=10**9+7

for i in range(n):

    for j in range(n):

        dp[i][i]=1

def dot(A,B):

    C=[[0]*n for i in range(n)]

    for i in range(n):

        for j in range(n):

            for k in range(n):

                C[i][j]+=A[i][k]*B[k][j]

            C[i][j]%=mod

    return C

while k:

    if k&1:

        dp=dot(dp,a)

    a=dot(a,a)

    k>>=1

ans=0

for i in dp:

    ans+=sum(i)

print((ans%mod))

# dp[n][i][j]=(頂点iから頂点jへ行く長さnのパスの個数)

# dp[n][i][j]=sum(dp[n-1][i][k]*a[k][j])⇒DP[n]=DP[n-1]*A DP[0]=Eより DP[n]=A^n

import sys

input = sys.stdin.readline



def dot(A,B,MOD=1000000007):

    N,M,L = len(A),len(A[0]),len(B[0])

    res = [[0]*L for i in range(N)]

    for i in range(N):

        for j in range(L):

            s = 0

            for k in range(M):

                s = (s + A[i][k]*B[k][j]) % MOD

            res[i][j] = s

    return res



def matPow(A,x,MOD=1000000007):

    N = len(A)

    res = [[0]*N for i in range(N)]

    for i in range(N):

        res[i][i] = 1

    for i in range(x.bit_length()):

        if (x>>i) & 1:

            res = dot(res,A)

        A = dot(A,A)

    return res



def main():

    n,k = list(map(int,input().split()))

    mod = 10**9+7

    a = [list(map(int,input().split())) for i in range(n)]

    dp = matPow(a,k)

    ans = 0

    for i in range(n):

        for j in range(n):

            ans += dp[i][j]

    print((ans%mod))



main()
# dp[n][i][j]=(頂点iから頂点jへ行く長さnのパスの個数)
# dp[n][i][j]=sum(dp[n-1][i][k]*a[k][j])⇒DP[n]=DP[n-1]*A DP[0]=Eより DP[n]=A^n
n, k = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(n)]
dp = [[0] * n for i in range(n)]
mod = 10**9 + 7
for i in range(n):
    for j in range(n):
        dp[i][i] = 1


def dot(A, B):
    C = [[0] * n for i in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                C[i][j] += A[i][k] * B[k][j]
            C[i][j] %= mod
    return C


while k:
    if k & 1:
        dp = dot(dp, a)
    a = dot(a, a)
    k >>= 1
ans = 0
for i in dp:
    ans += sum(i)
print((ans % mod))

# dp[n][i][j]=(頂点iから頂点jへ行く長さnのパスの個数)
# dp[n][i][j]=sum(dp[n-1][i][k]*a[k][j])⇒DP[n]=DP[n-1]*A DP[0]=Eより DP[n]=A^n
import sys

input = sys.stdin.readline


def dot(A, B, MOD=1000000007):
    N, M, L = len(A), len(A[0]), len(B[0])
    res = [[0] * L for i in range(N)]
    for i in range(N):
        for j in range(L):
            s = 0
            for k in range(M):
                s = (s + A[i][k] * B[k][j]) % MOD
            res[i][j] = s
    return res


def matPow(A, x, MOD=1000000007):
    N = len(A)
    res = [[0] * N for i in range(N)]
    for i in range(N):
        res[i][i] = 1
    for i in range(x.bit_length()):
        if (x >> i) & 1:
            res = dot(res, A)
        A = dot(A, A)
    return res


def main():
    n, k = list(map(int, input().split()))
    mod = 10**9 + 7
    a = [list(map(int, input().split())) for i in range(n)]
    dp = matPow(a, k)
    ans = 0
    for i in range(n):
        for j in range(n):
            ans += dp[i][j]
    print((ans % mod))


main()

-n, k = list(map(int, input().split())) -a = [list(map(int, input().split())) for i in range(n)] -dp = [[0] * n for i in range(n)] -mod = 10**9 + 7 -for i in range(n): -    for j in range(n): -        dp[i][i] = 1 +import sys + +input = sys.stdin.readline -def dot(A, B): -    C = [[0] * n for i in range(n)] +def dot(A, B, MOD=1000000007): +    N, M, L = len(A), len(A[0]), len(B[0]) +    res = [[0] * L for i in range(N)] +    for i in range(N): +        for j in range(L): +            s = 0 +            for k in range(M): +                s = (s + A[i][k] * B[k][j]) % MOD +            res[i][j] = s +    return res + + +def matPow(A, x, MOD=1000000007): +    N = len(A) +    res = [[0] * N for i in range(N)] +    for i in range(N): +        res[i][i] = 1 +    for i in range(x.bit_length()): +        if (x >> i) & 1: +            res = dot(res, A) +        A = dot(A, A) +    return res + + +def main(): +    n, k = list(map(int, input().split())) +    mod = 10**9 + 7 +    a = [list(map(int, input().split())) for i in range(n)] +    dp = matPow(a, k) +    ans = 0 -            for k in range(n): -                C[i][j] += A[i][k] * B[k][j] -            C[i][j] %= mod -    return C +            ans += dp[i][j] +    print((ans % mod)) -while k: -    if k & 1: -        dp = dot(dp, a) -    a = dot(a, a) -    k >>= 1 -ans = 0 -for i in dp: -    ans += sum(i) -print((ans % mod)) +main()