#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <bitset>
#include <cassert>
using namespace std;
typedef long long ll;

struct Mint {
	static const ll mod = (ll)(1e9 + 7);
	ll val;

	Mint() { val = 0; }
	Mint(ll a) {
		val = a;
		verify_value();
	}

	void verify_value() {
		if (val >= mod) val %= mod;
		if (val < 0) val %= mod, val += mod;
	}

	Mint pow(ll p) const {
		Mint cur = Mint(val), ret = 1;
		while (p > 0) {
			if (p & 1) ret *= cur;
			cur *= cur;
			p >>= 1LL;
		}
		return ret;
	}
	Mint inv() const {
		if (val == 0) cerr << "CAUTION: inv() is called with 0." << endl;
		return pow(mod - 2);
	}

	Mint operator+() const { return *this; }
	Mint operator-() const { return Mint(mod - val); }

	Mint operator+=(const Mint &a) {
		val += a.val;
		if (val >= mod) val -= mod;
		return Mint(val);
	}
	Mint operator*=(const Mint &a) {
		val *= a.val;
		if (val >= mod) val %= mod;
		return Mint(val);
	}
	Mint operator-=(const Mint &a) { return *this += -a; }
	Mint operator/=(const Mint &a) { return *this *= a.inv(); }

	Mint operator++() { return *this += Mint(1); }
	Mint operator--() { return *this -= Mint(1); }
	Mint operator++(int) {
		Mint ret = *this;
		++(*this);
		return ret;
	}
	Mint operator--(int) {
		Mint ret = *this;
		--(*this);
		return ret;
	}
};

Mint operator+(const Mint &a, const Mint &b) {
	ll ret = a.val + b.val;
	if (ret >= Mint::mod) ret -= Mint::mod;
	return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
	ll ret = a.val * b.val;
	if (ret >= Mint::mod) ret %= Mint::mod;
	return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }

ostream &operator<<(ostream &out, const Mint &a) { return out << a.val; }
istream &operator>>(istream &in, Mint &a) {
	in >> a.val;
	a.verify_value();
	return in;
}


// Matrix power
template<typename T>
class Matrix {
	vector<vector<T>> dat;
public:
	Matrix(int n, int m) { dat = vector<vector<T>>(n, vector<T>(m)); }
	Matrix(int n, int m, T init_val) { dat = vector<vector<T>>(n, vector<T>(m, init_val)); }
	Matrix(int n) : Matrix(n, n) {}
	Matrix(int n, T init_val) : Matrix(n, n, init_val) {}
	vector<T> &operator[](size_t idx) { return dat[idx]; }
	const vector<T> &operator[](size_t idx) const { return dat[idx]; }
	size_t size() const { return dat.size(); }
};
template<typename T>
ostream &operator<<(ostream &out, const Matrix<T> &a) {
	for (int i = 0; i < (int)a.size(); i++) {
		for (int j = 0; j < (int)a[i].size(); j++) {
			out << a[i][j] << " \n"[j == (int)a[i].size() - 1];
		}
	}
	return out;
}


template<typename T>
Matrix<T> matmul(const Matrix<T> &a, const Matrix<T> &b) {
	int n = (int)a.size();
	Matrix<T> ret(n);
	for (int i = 0; i < n; i++) {
		for (int k = 0; k < n; k++) {
			for (int j = 0; j < n; j++) {
				ret[i][j] += a[i][k] * b[k][j];
			}
		}
	}
	return ret;
}

template<typename T>
Matrix<T> matpow(Matrix<T> a, ll p) {
	int n = (int)a.size();
	Matrix<T> ret(n);
	for (int i = 0; i < n; i++) ret[i][i] = 1;
	while (p > 0) {
		if (p & 1) ret = matmul(ret, a);
		a = matmul(a, a);
		p >>= 1LL;
	}
	return ret;
}



int main() {
	int N; ll K; cin >> N >> K;
	Matrix<Mint> a(N);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) cin >> a[i][j];
	}
	a = matpow(a, K);
	Mint ans = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			ans += a[i][j];
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 384