import sys

sys.setrecursionlimit(2147483647)

INF = float("inf")

MOD = 10**9 + 7 # 998244353

input = lambda:sys.stdin.readline().rstrip()



def resolve():

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

    

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

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

    while p:

        if p & 1:

            dp = [[sum(dp[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)] for i in range(n)]

        base = [[sum(base[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)] for i in range(n)]

        p >>= 1



    print((sum(dp[i][j] for i in range(n) for j in range(n)) % MOD))

resolve()
import sys

sys.setrecursionlimit(2147483647)

INF = float("inf")

MOD = 10**9 + 7 # 998244353

input = lambda:sys.stdin.readline().rstrip()



def resolve():

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

    

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

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

    while p:

        if p & 1:

            dp = [[sum(dp[i][k] * base[k][j] % MOD for k in range(n)) % MOD for j in range(n)] for i in range(n)]

        base = [[sum(base[i][k] * base[k][j] % MOD for k in range(n)) % MOD for j in range(n)] for i in range(n)]

        p >>= 1



    print((sum(dp[i][j] for i in range(n) for j in range(n)) % MOD))

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7  # 998244353
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, p = list(map(int, input().split()))
    base = [list(map(int, input().split())) for _ in range(n)]
    dp = [[(i == j) + 0 for j in range(n)] for i in range(n)]
    while p:
        if p & 1:
            dp = [
                [sum(dp[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)]
                for i in range(n)
            ]
        base = [
            [sum(base[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)]
            for i in range(n)
        ]
        p >>= 1
    print((sum(dp[i][j] for i in range(n) for j in range(n)) % MOD))


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7  # 998244353
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, p = list(map(int, input().split()))
    base = [list(map(int, input().split())) for _ in range(n)]
    dp = [[(i == j) + 0 for j in range(n)] for i in range(n)]
    while p:
        if p & 1:
            dp = [
                [
                    sum(dp[i][k] * base[k][j] % MOD for k in range(n)) % MOD
                    for j in range(n)
                ]
                for i in range(n)
            ]
        base = [
            [
                sum(base[i][k] * base[k][j] % MOD for k in range(n)) % MOD
                for j in range(n)
            ]
            for i in range(n)
        ]
        p >>= 1
    print((sum(dp[i][j] for i in range(n) for j in range(n)) % MOD))


resolve()

-                [sum(dp[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)] +                [ +                    sum(dp[i][k] * base[k][j] % MOD for k in range(n)) % MOD +                    for j in range(n) +                ] -            [sum(base[i][k] * base[k][j] for k in range(n)) % MOD for j in range(n)] +            [ +                sum(base[i][k] * base[k][j] % MOD for k in range(n)) % MOD +                for j in range(n) +            ]