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

#include <vector>



using namespace std;



	

struct Matrix{

  static const int maxn = 105;

  long long int A[maxn][maxn];

  int n;

 

  Matrix(int n_): n(n_) {

    for(int i = 0; i < n; ++i)

      for(int j = 0; j < n; ++j)

        A[i][j] = 0;

  }

 

  Matrix operator * (const Matrix& o) const {

    Matrix ret(n);

    for(int i = 0; i < n; ++i)

      for(int j = 0; j < n; ++j)

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

          long long int c = A[i][k] * o.A[k][j] % 1000000007;

          ret.A[i][j] = (ret.A[i][j] + c) % 1000000007;

        }

    return ret;

  }

 

  static Matrix identity(int n){

    Matrix ret(n);

    for(int i = 0; i < n; ++i)

      ret.A[i][i] = 1;

    return ret;

  }

 

  Matrix operator ^ (long long k) const {

    Matrix ret = identity(n);

    Matrix a = *this;

    while(k){

      if(k & 1)ret = ret * a;

      a = a * a;

      k /= 2;

    }

    return ret;

  }

 

};



int main(void) {

    long long int N, K;

    cin >> N >> K;

    Matrix matrix(N);

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

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

            cin >> matrix.A[i][j];

        }

    }

    // for(int k=0; k<K-1; k++) {

    //     Matrix matrix_tmp(N, vector<int>(N));

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

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

    //             for(int l=0; l<N; l++) {

    //                 matrix_tmp[i][j] += matrix[i][l] *matrix[l][j];

    //             }

    //         }

    //     }

    //     matrix = matrix_tmp;

    // }

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

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

    //         cout << matrix[i][j] << " ";

    //     }

    //     cout << endl;

    // }

    matrix = matrix^(K);

    int ans = 0;

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

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

            ans = (ans + matrix.A[i][j]) % 1000000007;

        }

    }

    cout << ans << endl;

    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: