local n,k=io.read("n","n")
local a={}
for i=1,n do
    a[i]={}
    for j=1,n do
        a[i][j]=io.read("n")
    end
end

local mod=1000000007
local function mulmatrix(a,b)
    local A=#a
    local B=#b
    local C=#b[1]
    local c={}
    for i=1,A do
        c[i]={}
        for j=1,C do
            c[i][j]=0
        end
    end
    for i=1,A do
        for k=1,B do
            for j=1,C do
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod
            end
        end
    end
    return c
end

local function powmatrix(a,k)
    local A=#a
    local b={}
    for i=1,A do
        b[i]={}
        for j=1,A do
            b[i][j]=i==j and 1 or 0
        end
    end
    while k>0 do
        if k&1>0 then
            b=mulmatrix(b,a)
        end
        a=mulmatrix(a,a)
        k=k>>1
    end
    return b
end

local A=powmatrix(a,k)
local total=0
for i=1,n do
    for j=1,n do
        total=(total+A[i][j])%mod
    end
end
print(total)