/*
        Author: Arham_doshi
        @quote "Code like there's no tommorow!"
         ***************************************   UNAUTHORISED COPYING OF CODE PROHIBITED   **********************************
        */
        #include <bits/stdc++.h>
         
        #define int         long long
        #define double      long double
        #define pb          push_back
        #define pf          push_front
        #define pii         pair<int,int>
        #define vi          vector<int>
        #define vii         vector<pii>
        #define mi          map<int,int>
        #define mii         map<pii,int>
        #define all(a)      (a).begin(),(a).end()
        #define rall(a)     (a).rbegin(),(a).rend()
        #define x           first
        #define y           second
        #define sz(x)       (int)x.size()
        #define endl        '\n'
        #define hell        998244353
        #define PI          3.141592653589
        #define bs(v,n)     binary_search(all((v)),(n))
        #define lb(v,n)     lower_bound(all((v)),(n))
        #define ub(v,n)     upper_bound(all((v)),(n))
        #define fo(i,l,u)   for(i=l;i<u;i++)
        #define rfo(i,l,u)  for(i=l;i>=u;i--)
        #define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
        #define _init(b)     memset(b,-1,sizeof(b))
        #define _init0(b)     memset(b,0,sizeof(b))
        #define MOD         1000000007
         
         
         
        using namespace std;
         
        int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }  
        bool mod(double a,double b) {return a/b - floor(a/b);}
        
        int ceil(int n,int k){if(k%n==0){return k/n;}else return (k/n)+1;}
        
        int occurs(vi v,int n){
        auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
         
        int logb(int base, int x) {
            return (log(x) / log(base));
        }
        
        //int n;
    /*    void multiply(int n,int arr[][n] , int ary[][n]){
            int mat[n][n],i,j,k;
            fo(i,0,n){
                fo(j,0,n){
                    mat[i][j]=0;
                    fo(k,0,n){
                        mat[i][j]=(mat[i][j]+arr[i][k]*ary[k][j])%MOD;
                    }
                }
            }
            fo(i,0,n){
                fo(j,0,n){
                    //cout<<mat[i][j]<<" ";
                    arr[i][j]=mat[i][j];
                }
            }
            //cout<<endl;
        }
        
     */
     
        signed main()
        {
            ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
            int i,j,x,d,k,n,K;
            cin>>n>>K;
            int arr[n][n]={0};
            fo(i,0,n){
                fo(j,0,n){
                    cin>>x;
                    arr[i][j]=x;
                }
            }
            //cout<<K<<endl;
            int ans[n][n]={0};
            fo(i,0,n){
                fo(j,0,n){
                    ans[i][j]=0;
                }
            }
            fo(i,0,n){
                ans[i][i]=1;
            }
            
            
           
            
            
            while(K>0){
               
              /*  fo(i,0,n){
                            fo(j,0,n){
                                cout<<ans[i][j]<<" ";
                                //arr[i][j]=mat[i][j];
                            }
                            cout<<endl;
                        }*/
                                
            
               
                if(K%2==1){
                    //multiply(n,ans,arr);
                    
                    
                    int mat[n][n]={0};
                        fo(i,0,n){
                            fo(j,0,n){
                                mat[i][j]=0;
                                fo(k,0,n){
                                    mat[i][j]=(mat[i][j]+ans[i][k]*arr[k][j])%MOD;
                                    //cout<<mat[i][]
                                }
                               // cout<<mat[i][j]<<" ";
                            }
                            //cout<<endl;
                        }
                        fo(i,0,n){
                            fo(j,0,n){
                                //cout<<mat[i][j]<<" ";
                                ans[i][j]=mat[i][j];
                            }
                        }
                                
                                
                }
                
                // cout<<arr[0][0]<<endl;
                
                int mat[n][n]={0};
                        fo(i,0,n){
                            fo(j,0,n){
                                mat[i][j]=0;
                                fo(k,0,n){
                                    mat[i][j]=(mat[i][j]+arr[i][k]*arr[k][j])%MOD;
                                }
                            }
                        }
                        fo(i,0,n){
                            fo(j,0,n){
                                //cout<<mat[i][j]<<" ";
                                arr[i][j]=mat[i][j];
                            }
                        }
                                
                
                
             //   multiply(n,arr,arr);
                
                
                
                
                
                K/=2;
            }
            x=0;
            fo(i,0,n){
                fo(j,0,n){
                    x=(x+ans[i][j])%MOD;
                }
            }
            cout<<x;
            
            return 0;
        }

Measured memory_bytes: 384