/////////////////////////////////////////////////////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'               `$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//  
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$'                   `$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$'`$$$$$$$$$$$$$'`$$$$$$!                       !$$$$$$'`$$$$$$$$$$$$$'`$$$//
//$$$$  $$$$$$$$$$$  $$$$$$$                         $$$$$$$  $$$$$$$$$$$  $$$$//
//$$$$. `$' \' \$`  $$$$$$$!       $$        $$      !$$$$$$$  '$/ `/ `$' .$$$$//
//$$$$$. !\  i  i .$$$$$$$$       $$$$      $$$$      $$$$$$$$. i  i  /! .$$$$$//
//$$$$$$   `--`--.$$$$$$$$$        $$        $$       $$$$$$$$$.--'--'   $$$$$$//
//$$$$$$L        `$$$$$^^$$                           $$^^$$$$$'        J$$$$$$//
//$$$$$$$.   .'   ""~   $$$    $.                 .$  $$$   ~""   `.   .$$$$$$$//
//$$$$$$$$.  ;      .e$$$$$!    $$.             .$$  !$$$$$e,      ;  .$$$$$$$$//
//$$$$$$$$$   `.$$$$$$$$$$$$     $$$.         .$$$   $$$$$$$$$$$$.'   $$$$$$$$$//
//$$$$$$$$    .$$$$$$$$$$$$$!     $$`$$$$$$$$'$$    !$$$$$$$$$$$$$.    $$$$$$$$//
//$$$$$$$     $$$$$$$$$$$$$$$$.    $    $$    $   .$$$$$$$$$$$$$$$$     $$$$$$$//
//                                 $    $$    $								   //
//                                 $.   $$   .$								   //
//                                `$        $'								   //
//                                 `$$$$$$$$'								   //
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

#include <bits/stdc++.h>
using namespace std;

// Type-Define 
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef std::vector<char> vc;
typedef std::vector< vector<int> > vii;
typedef std::pair<int, int> pii;

// Hash-Define
#define int long long
#define MOD 1000000007
#define debugStart cerr << "\n[DEBUG START]\n"
#define debugStop cerr << "\n[DEBUG STOP]\n"
#define KHATAM cout << std::endl;
#define set_zero(x) memset(x, 0, sizeof(x));
#define ForPrint(x) std::for_each( begin(x), end(x), []( auto const& el ){ std::cout << el << " ";  } )

//Functions 
template<typename T> auto gcd ( T a, T b ) -> int  { return (b==0)?a:gcd(b,a%b); }

const int nax = 51;
int input[nax][nax];

const int inf = 1e18L + 5;
const int mod = 1e9 + 7; 

int mod_add ( int a, int b ) {

    int result = a + b;
    if( result >= mod ) {
        result -= mod; 
    }
    return result;
}

int mod_mul ( int a, int b ) {

    return ( ( a % mod ) * ( b % mod ) ) % mod;
}

void multiply ( int first[51][51], int second[51][51], int N ) {
    
   int new_mat[nax][nax];
   memset(new_mat, 0, sizeof(new_mat));
   for ( int i = 0; i < N; ++i ) {
    for ( int j = 0; j < N; ++j ) {
        for ( int k = 0; k < N; ++k ) {
            new_mat[i][j]= mod_add( new_mat[i][j], mod_mul( first[i][k], second[k][j] ) );
        }
    }
   }
   for( int i = 0; i < N; ++i ){
    for ( int j = 0; j < N; ++j ){
        first[i][j] = new_mat[i][j];
    }
   }
   // for ( int i = 0; i < N; ++i ) {
   //     int store[N];
   //     for( int c = 0; c < N; ++c ) {
   //         store[c] = first[i][c];
   //     }
   //     for ( int j = 0; j < N; ++j ) {
   //         int answer = 0;
   //         for ( int k = 0; k < N; ++k ) {
   //             answer = mod_add( answer,  mod_mul ( store[k], second[k][j]) );   
   //         }
   //         first[i][j] = answer;
   //     }
   // }
}

int solve ( int N, int K ) {

    int answer[nax][nax];
    memset(answer, 0, sizeof(answer));
    for( int i = 0; i < N; ++i ) 
        answer[i][i] = 1;
    
    while ( K ) {
        if ( K&1 ) {
            multiply(answer, input, N);
        }
        multiply( input, input, N );
        K >>= 1;
    }
    int sol = 0;
    for( int i = 0; i < N; ++i ) { 
        for( int j = 0; j < N; ++j ) {
            sol = mod_add(sol, answer[i][j]);
        }
    }
    return sol;
}

signed
main() {

    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1; 
    for ( int testcase = 1; testcase <= T; ++testcase ) {

        int N, K; std::cin >> N >> K;
        for ( int i = 0; i < N; ++i ) for( int j = 0; j < N; ++j ) {
            std::cin >> input[i][j];
        }
        std::cout << solve(N, K);
        KHATAM; 
    }
    return 0;
}


Measured memory_bytes: 384