/*  

    IITAPOORV094

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
|                                        |
|   *******     *******     ********     |
|      |           |            |        |
|      |           |            |        |
|      |           |            |        |
|   *******     *******         |        |
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

*/
  // This will work only for g++ compiler. 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>



#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based

//short hand for usual tokens
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)

// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())

//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())

// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())


using namespace std;

// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

vvll multiply(vvll A,vvll B)
{
	ll n=A.size();
	vvll res(n,vll(n,0));
	for0(i,n)
	{
		for0(j,n)
		{
			for0(k,n)
			{
				res[i][j]=(res[i][j]+A[i][k]*B[k][j])%MOD;
			}
		}
	}
	return(res);
}

vvll powd(vvll mat,ll exp)
{
	if(exp==1)
	{
		return(mat);
	}	
	if(exp%2==0)
	{
		vvll res=powd(mat,exp/2);
		return(multiply(res,res));
	}
	else
	{
		vvll res=powd(mat,exp-1);
		return(multiply(mat,res));
	}
}
ll solve(vvll &mat,ll n,ll k)
{
	vvll a=powd(mat,k);
	ll ans=0;
	for0(i,n)
	{
		for0(j,n)
		{
			ans=(ans+a[i][j])%MOD;
		}
	}
	return(ans);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    ll n,k;
    cin>>n>>k;
    vvll adj(n,vll(n,0));
    for0(i,n)
    {
    	for0(j,n)
    	{
    		cin>>adj[i][j];
    	}
    }
    cout<<solve(adj,n,k);
    return 0;
}


Measured memory_bytes: 2816