// Original C++ solution (slower):
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

#define sz(x) (int)(x).size()

#define all(x) (x).begin(), (x).end()

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



//#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



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

typedef pair <int, int> pii;



const int N = 53, MOD = 1e9 + 7;

int n;

ll k;



struct Mat {

    int a[N][N];

    Mat() {

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

    }

    void one() {

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

            a[i][i] = 1;

        }

    }

    Mat operator * (const Mat & m) const {

        Mat res;

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

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

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

                    res.a[i][k] += (ll)a[i][j] * m.a[j][k] % MOD;

                    if (res.a[i][k] >= MOD) {

                        res.a[i][k] -= MOD;

                    }

                }

            }

        }

        return res;

    }

    Mat binpow(ll x) {

        Mat res, num = * this;

        res.one();

        while (x > 0) {

            if (x & 1) {

                res = res * num;

            }

            x /= 2;

            num = num * num;

        }

        return res;

    }

};



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

    cin >> n >> k;

    Mat m;

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

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

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

        }

    }

    m = m.binpow(k);

    int res = 0;

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

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

            res += m.a[i][j];

            if (res >= MOD) {

                res -= MOD;

            }

        }

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


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

#define mod 1000000007



using namespace std;

long long n,k,total;

struct matrice

{

    vector < vector<long long>> t;

    matrice()

    {

        t.resize(n, vector<long long>(n));

    }



    matrice operator* (const matrice &b)

    {

        matrice ans = matrice();

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

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

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

                {

                    ans.t[i][k] += (t[i][j]*b.t[j][k])%mod;

                    ans.t[i][k] %= mod;

                }

        return ans;

    }

};

int main()

{

    cin>>n>>k;

    vector<vector<long long>> can(n, vector<long long>(n));

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

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

            cin>>can[i][j];



    matrice ans = matrice();

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

        ans.t[i][i]=1;



    matrice m = matrice();

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

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

            m.t[i][j]=can[i][j];



    while(k)

    {

        if(k%2)

        {

            ans=ans * m;

        }

        m = m*m;

        k/=2;

    }



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

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

        {

            total += ans.t[i][j];

            total %= mod;

        }



    cout<<total;

    return 0;

}


// Fastest known solution for this problem: