# coding: utf-8

# Your code here!

MOD=10**9+7



def dot(a, b):

    res = [[0] * n for _ in range(n)]

    for i in range(n):

        for k, aik in enumerate(a[i]):

            for j,bkj in enumerate(b[k]):

                res[i][j] += aik*bkj

                res[i][j] %= MOD

    return res





n,k = [int(i) for i in input().split()]

A = [[int(i) for i in input().split()] for _ in range(n)]



ans = [[0] * n for _ in range(n)]

for i in range(n):

    ans[i][i] = 1



while k:

    k, i = divmod(k, 2)

    if i:

        ans = dot(ans, A)

    A = dot(A, A)



res = 0

for i in range(n):

  for j in range(n):

    res += ans[i][j]

    res %= MOD

print(res)
# coding: utf-8

# Your code here!

MOD=10**9+7



def matmul(A, B, mod): # A,B: 行列

    res = [[0] * len(B[0]) for _ in range(len(A))]

    for i, resi in enumerate(res):

        for k, aik in enumerate(A[i]):

            for j,bkj in enumerate(B[k]):

                resi[j] += aik*bkj

                resi[j] %= mod

    return res



def matpow(A, p, M): #A^p mod M

    if p % 2 == 1:

        return matmul(A, matpow(A, p - 1, M), M)

    elif p > 0:

        b = matpow(A, p // 2, M)

        return matmul(b, b, M)

    else:

        return [[1 if i == j else 0 for j in range(len(A))] for i in range(len(A))]





n,k = [int(i) for i in input().split()]

A = [[int(i) for i in input().split()] for _ in range(n)]



ans = matpow(A,k,MOD)

print((sum(sum(r) % MOD for r in ans) % MOD))
# coding: utf-8
# Your code here!
MOD = 10**9 + 7


def dot(a, b):
    res = [[0] * n for _ in range(n)]
    for i in range(n):
        for k, aik in enumerate(a[i]):
            for j, bkj in enumerate(b[k]):
                res[i][j] += aik * bkj
                res[i][j] %= MOD
    return res


n, k = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for _ in range(n)]
ans = [[0] * n for _ in range(n)]
for i in range(n):
    ans[i][i] = 1
while k:
    k, i = divmod(k, 2)
    if i:
        ans = dot(ans, A)
    A = dot(A, A)
res = 0
for i in range(n):
    for j in range(n):
        res += ans[i][j]
        res %= MOD
print(res)

# coding: utf-8
# Your code here!
MOD = 10**9 + 7


def matmul(A, B, mod):  # A,B: 行列
    res = [[0] * len(B[0]) for _ in range(len(A))]
    for i, resi in enumerate(res):
        for k, aik in enumerate(A[i]):
            for j, bkj in enumerate(B[k]):
                resi[j] += aik * bkj
                resi[j] %= mod
    return res


def matpow(A, p, M):  # A^p mod M
    if p % 2 == 1:
        return matmul(A, matpow(A, p - 1, M), M)
    elif p > 0:
        b = matpow(A, p // 2, M)
        return matmul(b, b, M)
    else:
        return [[1 if i == j else 0 for j in range(len(A))] for i in range(len(A))]


n, k = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for _ in range(n)]
ans = matpow(A, k, MOD)
print((sum(sum(r) % MOD for r in ans) % MOD))

-def dot(a, b): -    res = [[0] * n for _ in range(n)] -    for i in range(n): -        for k, aik in enumerate(a[i]): -            for j, bkj in enumerate(b[k]): -                res[i][j] += aik * bkj -                res[i][j] %= MOD +def matmul(A, B, mod):  # A,B: 行列 +    res = [[0] * len(B[0]) for _ in range(len(A))] +    for i, resi in enumerate(res): +        for k, aik in enumerate(A[i]): +            for j, bkj in enumerate(B[k]): +                resi[j] += aik * bkj +                resi[j] %= mod + + +def matpow(A, p, M):  # A^p mod M +    if p % 2 == 1: +        return matmul(A, matpow(A, p - 1, M), M) +    elif p > 0: +        b = matpow(A, p // 2, M) +        return matmul(b, b, M) +    else: +        return [[1 if i == j else 0 for j in range(len(A))] for i in range(len(A))] -ans = [[0] * n for _ in range(n)] -for i in range(n): -    ans[i][i] = 1 -while k: -    k, i = divmod(k, 2) -    if i: -        ans = dot(ans, A) -    A = dot(A, A) -res = 0 -for i in range(n): -    for j in range(n): -        res += ans[i][j] -        res %= MOD -print(res) +ans = matpow(A, k, MOD) +print((sum(sum(r) % MOD for r in ans) % MOD))