#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;

//typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//conversion
inline LL toLong(string s) {LL v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define UNTIL(p) while(!(p))

//constant
const double EPS = 1e-5;
const double PI  = acos(-1.0);

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

const LL MOD = 1e9 + 7;
const LL M = 100;
LL n;
vector<vector<LL>> link;

LL dp[50][50][M+1] = {0};
map<LL,LL> memo[50][50];

LL f(LL i, LL j, LL k) {
    if(k <= M){
        return dp[i][j][k];
    }
    if(EXIST(memo[i][j], k)){
        return memo[i][j][k];
    }
    LL x = k/2;
    LL y = k - x;
    LL sum = 0;
    REP(t,n){
        sum += (f(i, t, x) * f(t, j, y)) % MOD;
        sum %= MOD;
    }
    memo[i][j].insert(MP(k, sum));
    return sum;
}

class Matrix{
public:
    vector<vector<LL>> a;
    int size;
    Matrix(int a_size):a(a_size, vector<LL>(a_size, 0)), size(a_size){}
    Matrix(const vector<vector<LL>>& a_a):a(a_a),size(a_a.size()){}
    Matrix(const Matrix& m):a(m.a),size(m.size){}
    Matrix& operator=(const Matrix& m){a = m.a; size = m.size; return *this;};
    Matrix operator+(const Matrix& m)const{
        Matrix t(size);
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                t.a[i][j] = a[i][j] + m.a[i][j];
        return t;
    }
    Matrix operator-(const Matrix& m)const{
        Matrix t(size);
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                t.a[i][j] = a[i][j] - m.a[i][j];
        return t;
    }
    Matrix operator*(const LL val)const{
        Matrix t(size);
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                t.a[i][j] = a[i][j] * val;
        return t;
    }
    Matrix operator*(const Matrix& m)const{
        Matrix t(size);
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                for(int k = 0; k < size; k++)
                    t.a[i][j] = (t.a[i][j] + a[i][k] * m.a[k][j]) % MOD;
        return t;
    }
    bool operator==(const Matrix& m)const{
        int size = a.size();
        for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                if(a[i][j] != m.a[i][j])return false;
        return true;
    }
    static Matrix i_mat(int a_size){
        Matrix t(a_size);
        for(int i = 0; i < a_size; i++)
            t.a[i][i] = 1;
        return t;
    }
    Matrix pow(LL n)const{
        Matrix p(*this);
        Matrix t = i_mat(size);
        while(n){
            if(n & 1) t = t * p;
            n = n >> 1;
            p = p * p;
        }
        return t;
    }
    void print()const{
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
                cout<< a[i][j] << " ";
            }
            cout<<endl;
        }
    }
};

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);

    LL k;
    cin>>n>>k;
    Matrix a(n);
    REP(i,n)REP(j,n)cin>>a.a[i][j];
    Matrix m = a.pow(k);
    LL ans = 0;
    REP(i,n)REP(j,n)ans = (ans + m.a[i][j]) % MOD;
    //link.resize(n);
    //REP(i,n){
    //    REP(j,n){
    //        LL a;
    //        cin>>a;
    //        if(a == 1)link[i].PB(j);
    //    }
    //}

    //REP(i,n)dp[i][i][0] = 1;
    //REP(len,M){
    //    REP(i,n){
    //        for(LL to : link[i]){
    //            REP(st, n){
    //                dp[st][to][len+1] += dp[st][i][len];
    //                dp[st][to][len+1] %= MOD;
    //            }
    //        }
    //    }
    //}

    //LL ans = 0;
    //REP(i,n)REP(j,n){
    //    ans += f(i, j, k);
    //    ans %= MOD;
    //}
    PUTS(ans);
}

Measured memory_bytes: 512