//#############################################################################
#include "bits/stdc++.h"
//#############################################################################
//Macros 
#define MOD 1000000007
#define INF 1000000000000009
#define eb emplace_back
#define mp make_pair
#define fr first
#define sc second
#define vec vector
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define sum(v) accumulate(all(v),0LL)
//Type Defs
typedef long long ll;
typedef unsigned long long ull;
//#############################################################################
using namespace std;
//#############################################################################
//Globals
//#############################################################################
//Helpers
void io_handler()
{
#ifdef LOCAL_TESTING
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
template <typename T>
void print_matrix(vector<vec<T>> mat){
    for(auto row: mat){
        for(auto e : row){
            // auto fst = e[0]==INF?"INF":to_string(e[0]);
            // auto scd = e[1]==INF?"INF":to_string(e[1]);
            // cout <<"["<<fst<<","<<scd<< "]"<<" ";
            cout<<e<<" ";
        }
        cout << endl;
    }
}
template <typename T>
void print_vector(vector<T> v){
    for(auto e : v){cout<< e <<" ";}
    cout<<endl;
}
template <typename T>
map<T,T> get_count(vector<T> v){
    map<T,T> count;
    for(auto e: v){
        if(count.find(e)!=count.end()){count[e]++;}
        else{count[e] = 1;}}
    return count;
}
void add_self(ll &a, ll b){
    a = a+b; if (a>=MOD){a -= MOD;}
}
ll sub_mod(ll a, ll b){
    a = (a-b+MOD)%MOD;
    return a;
}
//#############################################################################
//Solution
vec<vec<ll>> matrix_mul(vec<vec<ll>> a, vec<vec<ll>> b){
    int n = a.size();
    vec<vec<ll>> ans(n,vec<ll>(n,0LL));
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
            for(int k = 0; k<n; k++){
                add_self(ans[i][j],(a[i][k]*b[k][j])%MOD);
            }
        }
    }
    return ans;
}
vec<vec<ll>> mod_pow(vec<vec<ll>> &mat, ll e){
    int n = mat.size();
    vec<vec<ll>> ans(n,vec<ll>(n,0LL));
    for(int i = 0; i<n; i++){
        ans[i][i] = 1LL;
    }
    while(e){
        if(e&1){ans = matrix_mul(ans,mat);}
        mat = matrix_mul(mat,mat);
        e = e>>1;
    }
    return ans;
}
void solve(){
    /*
        dp[node] = no. of ways to reach that node following a path of k edges
    */
    int n; ll k;
    cin>>n>>k;
    vec<vec<ll>> adj(n,vec<ll>(n,0LL));
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++ ){
            cin>>adj[i][j];
        }
    }
    vec<vec<ll>> ans = mod_pow(adj,k);
    ll ret = 0LL;
    //print_matrix(ans);
    for(auto row : ans){for(auto e : row){add_self(ret,e);}}
    cout<<ret%MOD<<endl;
}
//#############################################################################
//Driver
int main()
{
    io_handler();
    int t = 1;
    //cin>>t;
    for (int i = 0; i < t; i++){
        solve();
    }
#ifdef LOCAL_TESTING
    cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}
//#############################################################################


Measured memory_bytes: 384