// Original C++ solution (slower):
#include <sstream>

#include <iostream>

#include <fstream>

#include <vector>

#include <algorithm>

#include <numeric>

#include <cmath>

#include <set>

#include <tuple>

#include <functional>

#include <map>

#include <unordered_map>

#include <queue>

#include <tuple>

#include <random>

#include <stack>



using namespace std;



#define endl '\n'



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



using lld = long double;

using ull = unsigned long int;

using ll = long long;



void solve() {

  int n; cin >> n;

  ll K; cin >> K;



  using matrix = vector<vector<ll>>;

  const ll mod = 1e9 + 7;



  auto mult_in =

    [](matrix& a, const matrix& b) -> void {

      matrix c(a.size(), vector<ll>(a.size()));

      for (int i = 0; i < (int)a.size(); ++i) {

        for (int j = 0; j < (int)a.size(); ++j) {

          c[i][j] = 0;

          for (int k = 0; k < (int)a.size(); ++k) c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;

        }

      }

      swap(a, c);

    };



  matrix b(n, vector<ll>(n));

  for (auto& v : b) for (auto& i : v) cin >> i;



  matrix a(n, vector<ll>(n));

  for (int i = 0; i < n; ++i) a[i][i] = 1;



  while (K) {

    if (K & 1) {

      mult_in(a, b);

    }

    mult_in(b, b);

    K /= 2;

  }





  ll ans = 0;

  for (auto& v : a) for (auto i : v) ans = (ans + i) % mod;



  cout << ans << endl;

}



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  const int precision = 13;

  cout.precision(precision);

  cout.setf(ios_base::fixed);





  solve();



  return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <algorithm>

#include <string>

#include <vector>

#include <numeric>

#include<queue>



const int mod = 1000000007;

//const int mod = 998244353;



class Matrix {

	int N;

	long long **d;

public:

	Matrix(int N) :N(N) {

		d = new long long*[N];

		for (int i = 0; i < N; i++) {

			d[i] = new long long[N] {};

		}

	}

	void set(long long** o) {

		for (int i = 0; i < N; i++) {

			for (int j = 0; j < N; j++) {

				d[i][j] = o[i][j];

			}

		}

	}

	void identity() {

		for (int i = 0; i < N; i++)d[i][i] = 1;

	}



	~Matrix() {

		for (int i = 0; i < N; i++) {

			delete[] d[i];

		}

		delete[] d;

	}

	long long* operator[](int x) const {

		return d[x];

	}

	Matrix& operator*=(const Matrix& o) {

		Matrix temp(N);

		for (int i = 0; i < N; i++) {

			for (int j = 0; j < N; j++) {

				for (int k = 0; k < N; k++) {

					temp[i][j] += d[i][k] * o[k][j];

					temp[i][j] %= mod;

				}

			}

		}

		for (int i = 0; i < N; i++) {

			for (int j = 0; j < N; j++) {

				d[i][j] = temp[i][j];

			}

		}

		return *this;

	}

	long long* operator*(long long* v) {

		long long* res = new long long[N] {};

		for (int i = 0; i < N; i++) {

			for (int j = 0; j < N; j++) {

				res[i] += d[i][j] * v[j];

			}

		}

		return res;

	}

	void print() {

		for (int i = 0; i < N; i++) {

			for (int j = 0; j < N; j++) {

				printf("%lld ", d[i][j]);

			}

			printf("\n");

		}

	}

};



int main() {

	int N;

	long long K;

	scanf("%d %lld", &N, &K);

	long long **G = new long long*[N];

	for (int i = 0; i < N; i++) {

		G[i] = new long long[N];

	}

	for (int i = 0; i < N; i++) {

		for (int j = 0; j < N; j++) {

			scanf("%lld", G[i] + j);

		}

	}

	Matrix g(N);

	g.set(G);

	Matrix r(N);

	r.identity();

	while (K != 0) {

		if (K & 1) {

			r *= g;

		}

		K >>= 1;

		g *= g;

	}

	long long res = 0;

	for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {

		res += r[i][j];

		res %= mod;

	}

	printf("%lld", res);

	return 0;

}


// Fastest known solution for this problem: