#include <bits/stdc++.h>

template<typename T>
class Matrix{
private:
	using size_type = ::std::size_t;
	using Row = ::std::vector<T>;
	using Mat = ::std::vector<Row>;

	size_type R, C; // row, column
	Mat A;

	void add_row_to_another(size_type r1, size_type r2, const T k){ // Row(r1) += Row(r2)*k
		for(size_type i = 0;i < C;i++)
			A[r1][i] += A[r2][i]*k;
	}

	void scalar_multiply(size_type r, const T k){
		for(size_type i = 0;i < C;i++)
			A[r][i] *= k;
	}

	void scalar_division(size_type r, const T k){
		for(size_type i = 0;i < C;i++)
			A[r][i] /= k;
	}

public:
	Matrix(){}
	Matrix(size_type r, size_type c) : R(r), C(c), A(r, Row(c)) {}
	Matrix(const Mat &m) : R(m.size()), C(m[0].size()), A(m) {}
	Matrix(const Mat &&m) : R(m.size()), C(m[0].size()), A(m) {}
	Matrix(const Matrix<T> &m) : R(m.R), C(m.C), A(m.A) {}
	Matrix(const Matrix<T> &&m) : R(m.R), C(m.C), A(m.A) {}
	Matrix<T> &operator=(const Matrix<T> &m){
 		R = m.R; C = m.C; A = m.A;
		return *this;
	}
	Matrix<T> &operator=(const Matrix<T> &&m){
 		R = m.R; C = m.C; A = m.A;
		return *this;
	}
	static Matrix I(const size_type N){
		Matrix m(N, N);
		for(size_type i = 0;i < N;i++) m[i][i] = 1;
		return m;
	}

	const Row& operator[](size_type k) const& { return A.at(k); }
	Row& operator[](size_type k) & { return A.at(k); }
	Row operator[](size_type k) const&& { return ::std::move(A.at(k)); }

	size_type row() const { return R; } // the number of rows
	size_type column() const { return C; }

	T determinant(){
		assert(R == C);
		Mat tmp = A;
		T res = 1;
		for(size_type i = 0;i < R;i++){
			for(size_type j = i;j < R;j++){ // satisfy A[i][i] > 0
				if (A[j][i] != 0) {
					if (i != j) res *= -1;
					swap(A[j], A[i]);
					break;
				}
			}
			if (A[i][i] == 0) return 0;
			res *= A[i][i];
			scalar_division(i, A[i][i]);
			for(size_type j = i+1;j < R;j++){
				add_row_to_another(j, i, -A[j][i]);
			}
		}
		swap(tmp, A);
		return res;
	}

	Matrix inverse(){
		assert(R == C);
		assert(determinant() != 0);
		Matrix inv(Matrix::I(R)), tmp(*this);
		for(size_type i = 0;i < R;i++){
			for(size_type j = i;j < R;j++){
				if (A[j][i] != 0) {
					swap(A[j], A[i]);
					swap(inv[j], inv[i]);
					break;
				}
			}
			inv.scalar_division(i, A[i][i]);
			scalar_division(i, A[i][i]);
			for(size_type j = 0;j < R;j++){
				if(i == j) continue;
				inv.add_row_to_another(j, i, -A[j][i]);
				add_row_to_another(j, i, -A[j][i]);
			}
		}
		(*this) = tmp;
		return inv;
	}

	Matrix& operator+=(const Matrix &B){
		assert(column() == B.column() && row() == B.row());
		for(size_type i = 0;i < R;i++)
			for(size_type j = 0;j < C;j++)
				(*this)[i][j] += B[i][j];
		return *this;
	}

	Matrix& operator-=(const Matrix &B){
		assert(column() == B.column() && row() == B.row());
		for(size_type i = 0;i < R;i++)
			for(size_type j = 0;j < C;j++)
				(*this)[i][j] -= B[i][j];
		return *this;
	}

	Matrix& operator*=(const Matrix &B){
		assert(R == B.row());
		Mat M(R, Row(C, 0));
		for(size_type i = 0;i < R;i++)
			for(size_type j = 0;j < B.column();j++)
				for(size_type k = 0;k < C;k++)
					M[i][j] += (*this)[i][k] * B[k][j];
		A.swap(M);
		return *this;
	}

	Matrix& operator/=(const Matrix &B){
		assert(C == B.row());
		Matrix M(B);
		(*this) *= M.inverse();
		return *this;
	}

	Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }
	Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }
	Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }
	Matrix operator/(const Matrix &B) const { return (Matrix(*this) /= B); }

	Matrix pow(size_type k){
		assert(R == C);
		Matrix M(Matrix::I(R));
		while(k){
			if (k & 1) M *= (*this);
			k >>= 1;
			(*this) *= (*this);
		}
		A.swap(M.A);
		return *this;
	}

	friend ::std::ostream &operator<<(::std::ostream &os, Matrix &p){
		for(size_type i = 0;i < p.row();i++){
			for(size_type j = 0;j < p.column();j++){
				os << p[i][j] << " ";
			}
			os << ::std::endl;
		}
		return os;
	}
};

template<::std::uint_fast64_t mod>
class ModInt{
private:
	using value_type = ::std::uint_fast64_t;
	value_type n;
public:
	ModInt() : n(0) {}
	ModInt(value_type n_) : n(n_ % mod) {}
	ModInt(const ModInt& m) : n(m.n) {}

	explicit operator value_type() const { return n; }

	friend ::std::ostream& operator<<(::std::ostream &os, const ModInt<mod> &a) {
		return os << a.n;
	}

	friend ::std::istream& operator>>(::std::istream &is, ModInt<mod> &a) {
		value_type x;
		is >> x;
		a = ModInt<mod>(x);
		return is;
	}

	bool operator==(const ModInt& m) const { return n == m.n; }
	bool operator!=(const ModInt& m) const { return n != m.n; }
	ModInt& operator*=(const ModInt& m){ n = n * m.n % mod; return *this; }

	ModInt pow(value_type b) const{
		ModInt ans = 1, m = ModInt(*this);
		while(b){
			if(b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}

	ModInt inv() const { return (*this).pow(mod-2); }
	ModInt& operator+=(const ModInt& m){ n += m.n; n = (n < mod ? n : n - mod); return *this; }
	ModInt& operator-=(const ModInt& m){ n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	ModInt& operator/=(const ModInt& m){ n = n * m.inv() % mod; return *this; }
	ModInt operator+(const ModInt& m) const { return ModInt(*this) += m; }
	ModInt operator-(const ModInt& m) const { return ModInt(*this) -= m; }
	ModInt operator*(const ModInt& m) const { return ModInt(*this) *= m; }
	ModInt operator/(const ModInt& m) const { return ModInt(*this) /= m; }
	ModInt& operator++(){ n += 1; return *this; }
	ModInt& operator--(){ n -= 1; return *this; }
	ModInt operator++(int){
		ModInt old(n);
		n += 1;
		return old;
	}
	ModInt operator--(int){
		ModInt old(n);
		n -= 1;
		return old;
	}
};
int main(void){
	using namespace std;
	using int32 = int_fast32_t;
	using int64 = int_fast64_t;
	const int64 mod = 1e9+7;
	int64 N, K;
	cin >> N >> K;
	Matrix<ModInt<mod>> M(N, N);
	for(int32 i = 0;i < N;i++)
		for(int32 j = 0;j < N;j++)
			cin >> M[i][j];
	M.pow(K);
	ModInt<mod> res = 0;
	for(int32 i = 0;i < N;i++)
		for(int32 j = 0;j < N;j++)
			res += M[i][j];
	cout << res << endl;
}


Measured memory_bytes: 256