You are given a simple directed graph G with N vertices. For each pair of vertices i and j, you are given an integer a_{i, j} representing the presence of a directed edge from vertex i to j. Find the number of different directed paths of length K in G, considering paths that traverse the same edge multiple times. The input consists of N, K, and the adjacency matrix a_{i, j}. Output the count of different directed paths modulo 10^9 + 7.

# Buggy version:
def mult(A, B):
    C = [[0 for i in range(len(A))] for i in range(len(A))]
 
    for i in range(len(A)):
        for j in range(len(A)):
            for k in range(len(A)):
                C[i][j] = (C[i][j]+A[i][k]*B[k][j])%mod
    
    return C
 
def exp(A, p):
    ans = [[0 for i in range(len(A))] for i in range(len(A))]
    for i in range(len(A)):
        ans[i][i] = 1
    
    while p:
        if p%2 == 0:
            A = mult(A, A)
            p = p//2
        else:
            ans = mult(ans, A)
            p -= 1
    
    return ans
 
n, k = map(int, input().strip().split())
grid = [list(map(int, input().strip().split())) for i in range(n)]
 
print(sum([sum(i)%mod for i in exp(grid, k)])%mod)

# Fixed version:
mod = 10**9+7
def mult(A, B):
    C = [[0 for i in range(len(A))] for i in range(len(A))]

    for i in range(len(A)):
        for j in range(len(A)):
            for k in range(len(A)):
                C[i][j] = (C[i][j]+A[i][k]*B[k][j])%mod
    
    return C

def exp(A, p):
    ans = [[0 for i in range(len(A))] for i in range(len(A))]
    for i in range(len(A)):
        ans[i][i] = 1
    
    while p:
        if p%2 == 0:
            A = mult(A, A)
            p = p//2
        else:
            ans = mult(ans, A)
            p -= 1
    
    return ans

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

print(sum([sum(i)%mod for i in exp(grid, k)])%mod)

# Buggy version:
import numpy as np
N,K=map(int,input().split())
a=[[int(i) for i in input().split()] for _ in range(N)]
a=np.array(a,dtype=np.int64)
MOD=10**9+7

def matmul(A,B,MOD):
    N=len(A)
    C=np.zeros(N,dtype=np.int64)
    for i in range(N):
        for j in range(N):
            for k in range(N):
            C[i][j] += A[i][k]B[k][j]%MOD
    return C

def mat_power(A,k,MOD):
    N=len(A)
    D=np.identity(N,dtype=np.int64)
    while k:
        if k & 1:
            D=matmul(D,A,MOD)
        A = matmul(A,A,MOD)
        k >>= 1

    return D

A=mat_power(a,K,MOD)
ans=0
for i in range(N):
    for j in range(N):
        ans=(ans+A[i][j])%MOD

print(ans)

# Fixed version:
import numpy as np
N,K=map(int,input().split())
a=[[int(i) for i in input().split()] for _ in range(N)]
a=np.array(a,dtype=np.int64)
MOD=10**9+7

def matmul(A,B,MOD):
    N=len(A)
    C=np.zeros((N,N),dtype=np.int64)

    for n in range(N):
        C[n] = (A[n,:][:,None] * B % MOD).sum(axis=0) % MOD
    return C

def mat_power(A,k,MOD):
    N=len(A)
    D=np.identity(N,dtype=np.int64)
    while k:
        if k & 1:
            D=matmul(D,A,MOD)
        A = matmul(A,A,MOD)
        k >>= 1

    return D

A=mat_power(a,K,MOD)
ans=0
for i in range(N):
    for j in range(N):
        ans=(ans+A[i][j])%MOD

print(ans)

# Buggy version:
N, K = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
mod = 10**9+7

def matrixmul(A, B):
    n = len(A)
    C = []
    for i in range(n):
        c = []
        for j in range(n):
            num = 0
            for k in range(n):
                num = (A[i][k]*B[k][j] + num)%mod
            c.append(num)
        C.append(c)
    return C

def matrixpow(A, n):
    if n == 1:
        return A
    if n&1 == 1:
        return matrixmul(matrixpow(A, n-1), A)
    else:
        return matrixmul(matrixpow(A, n//2), matrixpow(A, n//2))
    
An = matrixpow(A, K)
ans = 0
for i in range(N):
    for j in range(N):
        ans = (An[i][j]+ans)%mod
print(ans)

# Fixed version:
N, K = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
mod = 10**9+7

def matrixmul(A, B):
    n = len(A)
    C = []
    for i in range(n):
        c = []
        for j in range(n):
            num = 0
            for k in range(n):
                num = (A[i][k]*B[k][j] + num)%mod
            c.append(num)
        C.append(c)
    return C

now = [[1 if i == j else 0 for i in range(N)] for j in range(N)]        

while K:
    if K&1 == 1:
        now = matrixmul(now, A)
    K //= 2
    A = matrixmul(A, A)

ans = 0
for i in range(N):
    for j in range(N):
        ans = (now[i][j]+ans)%mod
print(ans)

# Buggy version:
N,K = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(N)]

mod = 10**9+7

import numpy as np

a = np.array(A)
Mat = np.eye(N)
print(Mat)
while K:
    if K%2:
        Mat = np.dot(Mat, a)%mod
    a = np.dot(a, a)%mod
    K //= 2
    
ans = 0
for i in range(N):
    for j in range(N):
        ans += int(Mat[i, j])
        ans %= mod
        
print(ans,a)

# Fixed version:
N,K = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(N)]

mod = 10**9+7

import numpy as np

a = np.array(A, dtype=object)
Mat = np.eye(N, dtype=object)

while K:
    if K%2:
        Mat = np.dot(Mat, a)%mod
    a = np.dot(a, a)%mod
    K //= 2
    
ans = 0
for i in range(N):
    for j in range(N):
        ans += Mat[i, j]
        ans %= mod
        
print(ans)

# Buggy version:
import numpy as np


def main():
    MOD = int(1e9 + 7)

    n, k = map(int, input().split())
    can = [[int(c) for c in input().split()] for _ in range(n)]
    can = np.array(can)

    answer = np.eye(n)
    while k:
        if k % 2:
            answer = np.dot(answer, can) % MOD
        can = np.dot(can, can) % MOD
        k //= 2

    total = 0
    for i in range(n):
        for j in range(n):
            total += answer[i,j]
            if total >= MOD:
                total -= MOD

    print(total)


if __name__ == "__main__":
    main()


# Fixed version:
import numpy as np


def main():
    MOD = int(1e9 + 7)

    n, k = map(int, input().split())
    can = [[int(c) for c in input().split()] for _ in range(n)]
    can = np.array(can, dtype=object)

    answer = np.eye(n, dtype=object)
    while k:
        if k % 2:
            answer = np.dot(answer, can) % MOD
        can = np.dot(can, can) % MOD
        k //= 2

    total = 0
    for i in range(n):
        for j in range(n):
            total += answer[i, j]
            if total >= MOD:
                total -= MOD

    print(total)


if __name__ == "__main__":
    main()

# Buggy version:
import numpy as np
mod = 10**9+7
def MatPower(A, k):
    B = np.identity(len(A), dtype = object)
    while k:
        if k&1:
            B = np.dot(A, B)%mod
        A = np.dot(A, A)%mod
        k//=2
    return B
N, K = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(N)], dtype = object)
v = np.array([1]*N, dtype = object).T
An = MatPower(a, K)
v = np.dot(An, v)%mod
print(v.sum())

# Fixed version:
import numpy as np
mod = 10**9+7
def MatPower(A, k):
    B = np.identity(len(A), dtype = object)
    while k:
        if k&1:
            B = np.dot(A, B)%mod
        A = np.dot(A, A)%mod
        k//=2
    return B
N, K = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(N)], dtype = object)
v = np.array([1]*N, dtype = object).T
An = MatPower(a, K)
v = np.dot(An, v)%mod
ans = 0
for i in range(N):
    ans+=v[i]
    ans%=mod
print(ans)

# Buggy version:
import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), dtype=Object)
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=grid
i=1
while 2**i<=K:
  #print(i)
  #dp.insert(1,grid*grid)
  gridsave=np.dot(dp[i-1][0],dp[i-1][0])
  gridsave=np.mod(gridsave,MOD)
  #print(gridsave)
  dp.append([gridsave])
  i+=1
i-=1
K-=2**i
i-=1
#print('dp',dp)
grid1=dp[len(dp)-1][0]
#print(grid1)
#print(i)
while K>0:
  if K>=2**i:
    #print('i',i)
    grid1=np.dot(grid1,dp[i][0])
    grid1=np.mod(grid1,MOD)
    K-=2**i
    #print('grid1',grid1)
  i-=1
  #print('K',K)
ans=0
#print(grid1)
for i in range(N):
  for j in range(N):
    ans=(ans+grid1[i][j])%MOD
print(ans)


# Fixed version:
import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), dtype='O')
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=grid
i=1
while 2**i<=K:
  #print(i)
  #dp.insert(1,grid*grid)
  gridsave=np.dot(dp[i-1][0],dp[i-1][0])
  gridsave=np.mod(gridsave,MOD)
  #print(gridsave)
  dp.append([gridsave])
  i+=1
i-=1
K-=2**i
i-=1
#print('dp',dp)
grid1=dp[len(dp)-1][0]
#print(grid1)
#print(i)
while K>0:
  if K>=2**i:
    #print('i',i)
    grid1=np.dot(grid1,dp[i][0])
    grid1=np.mod(grid1,MOD)
    K-=2**i
    #print('grid1',grid1)
  i-=1
  #print('K',K)
ans=0
#print(grid1)
for i in range(N):
  for j in range(N):
    ans=(ans+grid1[i][j])%MOD
print(ans)

# Buggy version:
import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), int)
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=np.eye(N,dtype=np.int64)
print(gridsave)
i=1
while K:
  if K & 1:
    gridsave=np.dot(grid,gridsave) % MOD
  grid=np.dot(grid,grid) % MOD
  K>>=1
ans=np.sum(gridsave)%MOD
print(gridsave)
print(ans)

# Fixed version:
import sys
import numpy as np
input = sys.stdin.readline

MOD=10**9+7
N, K = map(int, input().split())
grid = np.empty((0,N), dtype='O')
for i in range(N):
  array = list(map(int, input().strip().split()))
  array2 = np.array(array)
  grid = np.append(grid,[array2],axis=0)
#print(grid)
dp=[[grid]]
gridsave=np.eye(N,dtype="O")
#print(gridsave)
i=1
while K:
  if K & 1:
    gridsave=np.dot(grid,gridsave) % MOD
  grid=np.dot(grid,grid) % MOD
  K>>=1
ans=np.sum(gridsave)%MOD
#print(gridsave)
print(ans)