#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<int MOD>
class mod_int {
	unsigned x;
public:
	mod_int() : x(0) { }
	mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	mod_int &operator/=(mod_int that) { return *this *= that.inverse(); }

	mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
	mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
	mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
	mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }

	bool operator==(const mod_int& that) const { return x == that.x; }

	mod_int inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		return mod_int(u);
	}
};

template<int MOD>
istream& operator >> (istream& is, mod_int<MOD>& val) {
	long long x;
	is >> x; val = x;
	return is;
}

template<int MOD>
ostream& operator << (ostream& os, const mod_int<MOD>& val) {
	os << val.get();
	return os;
}

const int mod = 1e9 + 7;
using mint = mod_int<mod>;

template<class T>
class matrix {
	vector<vector<T>> dat;
public:
	matrix(int row) : dat(row, vector<T>(1)) {}
	matrix(int row, int col) : dat(row, vector<T>(col)) {}
	matrix(int row, int col, const T& id) : dat(row, vector<T>(col, id)) {}
	matrix(const vector<vector<T>>& v) : dat(v) {}
	matrix(const vector<T>& v) : dat(v.size(), vector<T>(1)) {
		for (int i = 0; i < (int)v.size(); ++i) dat[i][0] = v[i];
	}
	explicit operator vector<vector<T>>() const { return dat; }
	int row_size() const { return dat.size(); }
	int col_size() const { return dat[0].size(); }
	matrix<T>& operator+=(const matrix<T>& that) {
		int row = row_size(), col = col_size();
		assert(row_size() == that.row_size() && col_size() == that.col_size());
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] += that[i][j];
		return *this;
	}
	matrix<T>& operator-=(const matrix<T>& that) {
		int row = row_size(), col = col_size();
		assert(row_size() == that.row_size() && col_size() == that.col_size());
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] -= that[i][j];
		return *this;
	}
	matrix<T>& operator*=(const T& that) {
		int row = row_size(), col = col_size();
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] *= that;
		return *this;
	}
	matrix<T>& operator/=(const T& that) {
		int row = row_size(), col = col_size();
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] /= that;
		return *this;
	}
	matrix<T> operator*(const matrix<T>& that) const {
		int x = row_size(), y = col_size(), z = that.col_size();
		assert(col_size() == that.row_size());
		matrix<T> res(x, z);
		for (int i = 0; i < x; ++i) {
			for (int j = 0; j < y; ++j) {
				for (int k = 0; k < z; ++k) {
					res[i][k] += dat[i][j] * that[j][k];
				}
			}
		}
		return res;
	}
	matrix<T>& operator*=(const matrix<T>& that) { return *this = *this * that; }

	vector<T>& operator[](size_t i) & { return dat[i]; }
	const vector<T>& operator[](size_t i) const& { return dat[i]; }
	vector<T> operator[](size_t i) const&& { return move(dat[i]); }

	T& get(int i, int j) & {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return dat[i][j];
	}
	const T& get(int i, int j) const& {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return dat[i][j];
	}
	T get(int i, int j) const&& {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return move(dat[i][j]);
	}
	matrix<T> transpose() const {
		int row = row_size(), col = col_size();
		matrix<T> res(col, row);
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) res[j][i] = dat[i][j];
		return res;
	}
};

template <class T> matrix<T> operator+(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) += b; }
template <class T> matrix<T> operator-(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) -= b; }
template <class T> matrix<T> operator*(const matrix<T>& a, const T& b) { return matrix<T>(a) *= b; }
template <class T> matrix<T> operator*(const T& a, const matrix<T>& b) { return matrix<T>(b) *= a; }
template <class T> matrix<T> operator/(const matrix<T>& a, const T& b) { return matrix<T>(a) /= b; }

template <class T> matrix<T> identity_matrix(int n) {
	matrix<T> a(n, n);
	for (int i = 0; i < n; i++) a[i][i] = T(1);
	return a;
}

template <class T> matrix<T> pow(const matrix<T>& a, long long b) {
	const int n = a.row_size();
	assert(a.row_size() == a.col_size() && 0 <= b);
	matrix<T> res = identity_matrix<T>(n), x(a);
	while (true) {
		if (b & 1LL) res *= x;
		if (!(b >>= 1LL)) break;
		x *= x;
	}
	return res;
}

int main()
{
	int N;
	ll K;
	cin >> N >> K;
	matrix<mint> a(N, N);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> a[i][j];
		}
	}
	a = pow(a, K);
	mint res = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			res += a[i][j];
		}
	}
	cout << res << endl;
	return 0;
}


Measured memory_bytes: 256