#include <bits/stdc++.h>
using namespace std;
//#define long long long
#define itn int

// {{{ ModMatrix
struct Matrix {

    int MOD;
    int size;
    vector<vector<int> > data;

    Matrix ( int n, int mod = (int)1e9+7 ) {
        data.assign(n, vector<int>(n, 0));
        size = n;
        MOD = mod;
    }

    Matrix operator + ( const Matrix &d ) const {
        Matrix ans(size, MOD);
        for ( int i = 0; i < size; i++ ) {
            for ( int j = 0; j < size; j++ ) {
                ans[i][j] = ( (long long)data[i][j] + d[i][j] ) % MOD;
            }
        }
        return ans;
    }

    Matrix operator-() const {
        Matrix ans(size, MOD);
        for ( int i = 0; i < size; i++ ) {
            for ( int j = 0; j < size; j++ ) {
                ans[i][j] = -data[i][j];
            }
        }
        return ans;
    }

    Matrix operator - ( const Matrix &d ) const {
        return *this + (-d);
    }

    Matrix operator * ( const Matrix &d ) const {
        Matrix ans(size, MOD);
        for ( int i = 0; i < size; i++ ) {
            for ( int j = 0; j < size; j++ ) {
                for ( int k = 0; k < size; k++ ) {
                    ans[i][j] = ((long long)ans[i][j] +
                            ( (long long)data[i][k] * d[k][j] ) ) % MOD;
                }
            }
        }
        return ans;
    }

    Matrix operator * ( const int m ) const {
        Matrix ans(size, MOD);
        for ( int i = 0; i < size; i++ ) {
            for ( int j = 0; j < size; j++ ) {
                ans[i][j] = data[i][j] * m;
            }
        }
        return ans;
    }

    Matrix &operator += ( const Matrix &d ) {
        *this = *this + d;
        return *this;
    }

    Matrix &operator -= ( const Matrix &d ) {
        *this = *this - d;
        return *this;
    }

    Matrix &operator *= ( const Matrix &d ) {
        *this = *this * d;
        return *this;
    }

    Matrix &operator *= ( const int m ) {
        *this = *this * m;
        return *this;
    }

    Matrix operator ^ ( long long r ) {
        Matrix ans = I();
        Matrix x = *this;
        while ( 0 < r ) {
            if ( r & 1 ) {
                ans *= x ;
            }
            x *= x;
            r >>= 1;
        }
        return ans;
    }

    Matrix &operator ^= ( const long long r ) {
        *this = *this ^ r;
        return *this;
    }

    inline const vector<int>& operator [] ( const int i ) const {
        return data[i];
    }

    inline vector<int>& operator [] ( const int i ) {
        return data[i];
    }

    Matrix I() const {
        Matrix ans(size, MOD);
        for ( int i = 0; i < size; i++ ) {
            ans[i][i] = 1;
        }
        return ans;
    }

};
// }}}

signed main ( ) {

    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    long long p;
    cin >> n >> p;
    Matrix mat(n);

    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cin >> mat[i][j];
        }
    }

    mat ^= p;
    int ans = 0;
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            ans = ( ans + mat.data[i][j] ) % ((int)1e9 + 7);
        }
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256