#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

struct matrix {
    vector<vector<ll>> elements;
    int height;
    int width;

    matrix (vector<vector<ll>> es) {
        elements = es;
        height = (int)es.size();
        if (height > 0) width = (int)es[0].size();
        else width = 0;
        return;
    }

    matrix() {
        matrix({});
    }

    matrix(const int n) {
        elements = vector<vector<ll>>(n, vector<ll>(n, 0));
        for (int i = 0; i < n; i++) elements[i][i] = 1;
        height = n;
        width = n;
    }

    matrix(const int h, const int w) {
        elements = vector<vector<ll>>(h, vector<ll>(w, 0));
        for (int i = 0; i < min(h, w); i++) elements[i][i] = 1;
        height = h;
        width = w;
    }

    void print() {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                printf("%lld ", elements[i][j]);
            }
            printf("\n");
        }
        return;
    }
    
    void set(const matrix a) {
        elements = a.elements;
        height = (int)elements.size();
        if (height > 0) width = (int)elements[0].size();
        else width = 0;
        return;
    }

    /*
    void operator=(const matrix a) {
        elements = a.elements;
        height = (int)elements.size();
        if (height > 0) width = (int)elements[0].size();
        else width = 0;
        return;
    }*/

    void operator*=(ll times) {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                elements[i][j] *= times;
            }
        }
        return;
    }

    void operator%=(ll mod) {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                elements[i][j] %= mod;
            }
        }
        return;
    }

    vector<ll> operator[](ll i) {
        return elements[i];
    }

};

matrix add(const matrix a, const matrix b, const ll mod) {
    assert (a.height == b.height);
    assert (a.width == b.width);

    int new_h = a.height, new_w = a.width;
    matrix c(new_h, new_w);
    for (int i = 0; i < new_h; i++) {
        for (int j = 0; j < new_w; j++) {
            c.elements[i][j] = (a.elements[i][j] + b.elements[i][j]) % mod;
        }
    }
    return c;
}

matrix mul(const matrix a, const matrix b, const ll mod) {
    assert (a.width == b.height);

    int new_h = a.height, new_w = b.width;
    matrix c(new_h, new_w);
    for (int i = 0; i < new_h; i++) {
        for (int j = 0; j < new_w; j++) {
            ll val = 0;
            for (int k = 0; k < a.height; k++) {
                val += a.elements[i][k] * b.elements[k][j];
                val %= mod;
            }
            c.elements[i][j] = val;
        }
    }
    return c;
}

matrix pow(const matrix b, const ll q, const ll mod) {
    assert (b.height == b.width);

    matrix a(b.elements);
    ll r = q;
    matrix res(a.height);
    while (r > 0) {
        if (r&1) {
            res.set(mul(res, a, mod));
        }
        a.set(mul(a, a, mod));
        r >>= 1;
    }

    return res;
}

int main() {
    int N;
    ll K;
    cin >> N >> K;
    vector<vector<ll>> vv(N, vector<ll>(N, 0));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) cin >> vv[i][j];
    }
    
    
    matrix mtr(vv);
    ll mod = 1e9+7;
    mtr.set(pow(mtr, K, mod));
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) ans += mtr[i][j];
    }
    cout << ans%mod << endl;
    return 0;
}

Measured memory_bytes: 3780