// Slower C++ solution:
#include <bits/stdc++.h>

 

#define ll long long

#define X first

#define Y second

#define MP make_pair

 

using namespace std;



const int N = 123;

const ll mod = 1e9 + 7;             



int n;

struct mtx{

	ll a[N][N], len = n;

	mtx(){

		memset(a, 0, sizeof(a));

	}

	void operator *= (mtx b){

		int res[N][N];

		memset(res, 0, sizeof(res));

		for(int i = 1;i <= len;i++){

			for(int j = 1;j <= len;j++){

				for(int k = 1;k <= len;k++){

					res[i][j] = (res[i][j] + a[i][k] * b.a[k][j]) % mod;

				}

			}

		}

		for(int i = 1;i <= len;i++){

			for(int j = 1;j <= len;j++){

				a[i][j] = res[i][j];

			}

		}

	}

};



mtx bin_pow(mtx x, ll y){

	mtx res;

	for(int i = 1;i <= n;i++){

			res.a[i][i] = 1;

	}

    

	while(y){

		if(y & 1LL)

			res *= x;

		

	/*	for(int i = 1;i <= n;i++){	

			for(int j = 1;j <= n;j++){

				cout << res.a[i][j] << " ";

	    	}

			cout << "\n";

		}

	*/	

		x *= x;

		y >>= 1LL;

	}

	return res;

}

   

int main () {

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);



	ll k;

	cin >> n >> k;

	mtx z;

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

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

			cin >> z.a[i][j];

	//z *= z;

	z = bin_pow(z, k);

	ll res = 0;

	for(int i = 1;i <= n;i++){	

		for(int j = 1;j <= n;j++){

		//	cout << z.a[i][j] << " ";

			res += z.a[i][j], res %= mod;

	    }

		//cout << "\n";

	}

	cout << res;

}

// Faster C++ solution for the same problem (measured speedup: 5.018040149904893x):
/*

Luis Henrique Morelli

Walk

https://atcoder.jp/contests/dp/tasks/dp_r

10/09/2020

*/



#include <bits/stdc++.h>



#define MAX 50

#define MOD 1000000007



using namespace std;



void multiplyMatrix(int N, long long a[][MAX], long long b[][MAX]){

    long long c[MAX][MAX];



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

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

            c[i][j] = 0;

            

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

                c[i][j] = (c[i][j] % MOD + (a[i][k] % MOD * b[k][j] % MOD) % MOD) % MOD;

            }

        }

    }



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

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

            b[i][j] = c[i][j];

        }

    }

}



long long matrixPower(int N, long long g[][MAX], long long K){

    long long paths[MAX][MAX];



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

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

            if(i != j){

                paths[i][j] = 0;

            }

        }



        paths[i][i] = 1;

    }



    while(K > 0){

        if(K % 2 != 0){

            multiplyMatrix(N, g, paths);

        }



        multiplyMatrix(N, g, g);



        K /= 2;

    }



    long long totalPaths;



    totalPaths = 0;



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

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

            totalPaths = (totalPaths % MOD + paths[i][j] % MOD) % MOD; 

        }

    }



    return totalPaths % MOD;

}



int main(){

    ios::sync_with_stdio(0);

    cin.tie(0);

   

    int N;

    long long K;



    cin >> N >> K;



    long long adjMatrix[MAX][MAX];



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

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

           cin >> adjMatrix[i][j];

        }

    }



    cout << matrixPower(N, adjMatrix, K) << "\n";

    

    return 0;

}