#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct matrix: vector<vector<long long>>{
	int N, M;
	const long long mod;
	matrix(int N, int M, long long mod, bool is_id = false): N(N), M(M), mod(mod){
		resize(N, vector<long long>(M));
		if(is_id) for(int i = 0; i < min(N, M); ++ i) (*this)[i][i] = 1;
	}
	matrix(const vector<vector<long long>> &arr, long long mod): N(arr.size()), M(arr[0].size()), mod(mod){
		resize(N);
		for(int i = 0; i < N; ++ i) (*this)[i] = arr[i];
	}
	bool operator==(const matrix &otr) const{
		if(N != otr.N || M != otr.M) return false;
		for(int i = 0; i < N; ++ i) for(int j = 0; j < M; ++ j) if((*this)[i][j] != otr[i][j]) return false;
		return true;
	}
	matrix &operator=(const matrix &otr){
		N = otr.N, M = otr.M;
		resize(N);
		for(int i = 0; i < N; ++ i) (*this)[i] = otr[i];
		return *this;
	}
	matrix operator+(const matrix &otr) const{
		matrix res(N, M, mod);
		for(int i = 0; i < N; ++ i) for(int j = 0; j < M; ++ j) res[i][j] = ((*this)[i][j] + otr[i][j]) % mod;
		return res;
	}
	matrix &operator+=(const matrix &otr){
		return *this = *this + otr;
	}
	matrix operator*(const matrix &otr) const{
		assert(M == otr.N);
		int L = otr.M;
		matrix res(N, M, mod);
		for(int i = 0; i < N; ++ i) for(int j = 0; j < L; ++ j) for(int k = 0; k < M; ++ k) (res[i][j] += (*this)[i][k] * otr[k][j]) %= mod;
		return res;
	}
	matrix &operator*=(const matrix &otr){
		return *this = *this * otr;
	}
	matrix operator^(long long e) const{
		assert(N == M);
		matrix res(N, N, mod, 1), b(*this);
		for(; e; b *= b, e >>= 1) if(e & 1) res *= b;
		return res;
	}
	matrix &operator^=(const long long e){
		return *this = *this ^ e;
	}
	long long det() const{
		assert(N == M);
		vector<vector<long long>> temp = *this;
		long long res = 1;
		for(int i = 0; i < N; ++ i){
			for(int j = i + 1; j < N; ++ j){
				while(temp[j][i]){
					long long t = temp[i][i] / temp[j][i];
					if(t) for(int k = i; i < N; ++ k) temp[i][k] = (temp[i][k] - temp[j][k] * t) % mod;
					std::swap(temp[i], temp[j]);
					res *= -1;
				}
			}
			res = (res + mod) * temp[i][i] % mod;
			if(!res) return 0;
		}
		return res;
	}
};

int main(){
	cin.tie(0)->sync_with_stdio(0);
	const long long mod = 1e9 + 7;
	int n;
	long long k;
	cin >> n >> k;
	vector<vector<long long>> a(n, vector<long long>(n));
	for(auto i = 0; i < n; ++ i){
		for(auto j = 0; j < n; ++ j){
			cin >> a[i][j];
		}
	}
	matrix M(a, mod);
	M ^= k;
	long long res = 0;
	for(auto i = 0; i < n; ++ i){
		for(auto j = 0; j < n; ++ j){
			res = (res + M[i][j]) % mod;
		}
	}
	cout << res;
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////

Measured memory_bytes: 384