#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

template <typename T, T MOD = 1000000007>
struct ModInt {
	T x;
	ModInt() :x(0) {}
	ModInt(long long y) { x = y % MOD; if (x < 0) x += MOD; }

	ModInt& operator+=(const ModInt& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt& operator-=(const ModInt& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt& operator*=(const ModInt& a) {
		x = 1LL * x * a.x % MOD;
		return *this;
	}
	ModInt& operator/=(const ModInt& a) {
		*this *= a.inv();
		return *this;
	}

	ModInt operator+(const ModInt& a) const { return ModInt(x) += a; }
	ModInt operator-(const ModInt& a) const { return ModInt(x) -= a; }
	ModInt operator*(const ModInt& a) const { return ModInt(x) *= a; }
	ModInt operator/(const ModInt& a) const { return ModInt(x) /= a; }

	ModInt operator-() const { return ModInt(-x); }

	bool operator==(const ModInt a) const { return x == a.x; }
	bool operator!=(const ModInt a) const { return x != a.x; }
	bool operator<(const ModInt a) const { return x < a.x; }

	friend ostream& operator<<(ostream& os, const ModInt<T>& a) {
		return os << a.x;
	}
	friend istream& operator>>(istream& is, ModInt<T>& a) {
		T t; is >> t;
		a = ModInt(t);
		return is;
	}

	ModInt inv() const {
		T a = x, b = MOD, u = 1, v = 0;
		while (b > 0) {
			T t = a / b;
			swap((a -= t * b), b);
			swap((u -= t * v), v);
		}
		return ModInt(u);
	}

	ModInt pow(long long n) const {
		ModInt res(1), tmp(x);
		while (n > 0) {
			if (n & 1) res *= tmp;
			tmp *= tmp;
			n >>= 1;
		}
		return res;
	}

	static ModInt comb(long long n, long long k) {
		ModInt num(1), den(1);
		for (int i = 0; i < k; ++i) {
			num *= ModInt(n - i);
			den *= ModInt(i + 1);
		}
		return num / den;
	}
};

template <typename T>
class Matrix {
public:
	vector<vector<T> > A;

	Matrix() {}
	Matrix(size_t n) :A(n, vector<T>(n, 0)) {}
	Matrix(size_t n, size_t m) :A(n, vector<T>(m, 0)) {}
	Matrix(vector<vector<T> > A) :A(A) {}

	vector<T>& operator[](size_t k) { return A[k]; }
	const vector<T>& operator[](size_t k) const { return A[k]; }

	Matrix& operator+=(const Matrix& B) {
		size_t n = height(), m = width();
		for (size_t i = 0; i < n; ++i)
			for (size_t j = 0; j < m; ++j)
				(*this)[i][j] += B[i][j];
		return *this;
	}
	Matrix& operator-=(const Matrix& B) {
		size_t n = height(), m = width();
		for (size_t i = 0; i < n; ++i)
			for (size_t j = 0; j < m; ++j)
				(*this)[i][j] -= B[i][j];
		return *this;
	}
	Matrix& operator*=(const Matrix& B) {
		size_t n = height(), m = B.width(), p = width();
		vector<vector<T> > C(n, vector<T>(m));
		for (size_t i = 0; i < n; ++i)
			for (size_t j = 0; j < m; ++j)
				for (size_t k = 0; k < p; ++k)
					C[i][j] += (*this)[i][k] * B[k][j];
		A.swap(C);
		return *this;
	}

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

	size_t height() const { return A.size(); }
	size_t width() const { return A[0].size(); }

	static Matrix Identity(size_t n) {
		Matrix I(n);
		for (size_t i = 0; i < n; ++i) I[i][i] = 1;
		return I;
	}
	
	Matrix pow(long long n) const {
		Matrix res = Matrix::Identity(height()), tmp(A);
		while (n > 0) {
			if (n & 1) res *= tmp;
			tmp *= tmp;
			n >>= 1;
		}
		return res;
	}
};

int main() {
	long long n, k; cin >> n >> k;
	Matrix<ModInt<long long> > mat(n);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j) cin >> mat[i][j];
	mat = mat.pow(k);
	ModInt<long long> ans;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)	ans += mat[i][j];
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 384