import sys



input = sys.stdin.readline





def dot(a, b):

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

    for i, ai in enumerate(a):

        for j, bj in enumerate(zip(*b)):

            c[i][j] = sum(ak * bk for ak, bk in zip(ai, bj)) % md

    return c





def pwr(a, k):

    c = None

    while c == None:

        if k & 1:

            c = a

        a = dot(a, a)

        k >>= 1



    while k:

        if k & 1:

            c = dot(a, c)

        a = dot(a, a)

        k >>= 1

    return c





md = 10 ** 9 + 7

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

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

print((sum([sum(pk) % md for pk in pwr(a, k)]) % md))

import numpy as np

import sys



input = sys.stdin.readline



md = 10 ** 9 + 7

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

a = np.array([list(map(int, input().split())) for _ in range(n)], dtype="O")

c = np.eye(n, dtype="O")



while k:

    if k & 1:

        c = np.dot(a, c) % md

    a = np.dot(a, a) % md

    k >>= 1

print((np.sum(c) % md))

import sys

input = sys.stdin.readline


def dot(a, b):
    c = [[0] * n for _ in range(n)]
    for i, ai in enumerate(a):
        for j, bj in enumerate(zip(*b)):
            c[i][j] = sum(ak * bk for ak, bk in zip(ai, bj)) % md
    return c


def pwr(a, k):
    c = None
    while c == None:
        if k & 1:
            c = a
        a = dot(a, a)
        k >>= 1
    while k:
        if k & 1:
            c = dot(a, c)
        a = dot(a, a)
        k >>= 1
    return c


md = 10**9 + 7
n, k = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
print((sum([sum(pk) % md for pk in pwr(a, k)]) % md))

import numpy as np
import sys

input = sys.stdin.readline
md = 10**9 + 7
n, k = list(map(int, input().split()))
a = np.array([list(map(int, input().split())) for _ in range(n)], dtype="O")
c = np.eye(n, dtype="O")
while k:
    if k & 1:
        c = np.dot(a, c) % md
    a = np.dot(a, a) % md
    k >>= 1
print((np.sum(c) % md))

+import numpy as np - - -def dot(a, b): -    c = [[0] * n for _ in range(n)] -    for i, ai in enumerate(a): -        for j, bj in enumerate(zip(*b)): -            c[i][j] = sum(ak * bk for ak, bk in zip(ai, bj)) % md -    return c - - -def pwr(a, k): -    c = None -    while c == None: -        if k & 1: -            c = a -        a = dot(a, a) -        k >>= 1 -    while k: -        if k & 1: -            c = dot(a, c) -        a = dot(a, a) -        k >>= 1 -    return c - - -a = [list(map(int, input().split())) for _ in range(n)] -print((sum([sum(pk) % md for pk in pwr(a, k)]) % md)) +a = np.array([list(map(int, input().split())) for _ in range(n)], dtype="O") +c = np.eye(n, dtype="O") +while k: +    if k & 1: +        c = np.dot(a, c) % md +    a = np.dot(a, a) % md +    k >>= 1 +print((np.sum(c) % md))