Problem:
Score : 100 points

Problem Statement

There is a simple directed graph G with N vertices, numbered 1, 2, \ldots, N.

For each i and j (1 \leq i, j \leq N), you are given an integer a_{i, j} that represents whether there is a directed edge from Vertex i to j.
If a_{i, j} = 1, there is a directed edge from Vertex i to j; if a_{i, j} = 0, there is not.

Find the number of different directed paths of length K in G, modulo 10^9 + 7.
We will also count a path that traverses the same edge multiple times.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 10^{18}

a_{i, j} is 0 or 1.

a_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of different directed paths of length K in G, modulo 10^9 + 7.

Sample Input 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

Sample Output 1

6

G is drawn in the figure below:

There are six directed paths of length 2:

1 → 2 → 3

1 → 2 → 4

2 → 3 → 4

2 → 4 → 1

3 → 4 → 1

4 → 1 → 2

Sample Input 2

3 3
0 1 0
1 0 1
0 0 0

Sample Output 2

3

G is drawn in the figure below:

There are three directed paths of length 3:

1 → 2 → 1 → 2

2 → 1 → 2 → 1

2 → 1 → 2 → 3

Sample Input 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3

1

G is drawn in the figure below:

There is one directed path of length 2:

4 → 5 → 6

Sample Input 4

1 1
0

Sample Output 4

0

Sample Input 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5

957538352

Be sure to print the count modulo 10^9 + 7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007; 
const int INF = 1000000000; //1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const long double EPS = 1E-10;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}



template <class T> class Matrix{
private:
    std::vector<std::vector<T>> A;
public:
    Matrix(){}

    Matrix(size_t m, size_t n): A(m, std::vector<T>(n, 0)) {}

    Matrix(size_t n): A(n, std::vector<T>(n, 0)) {}

    Matrix(size_t m, size_t n, T a): A(m, std::vector<T>(n, a)) {}

    Matrix(const Matrix& mat){if(this != &mat) A = mat.A;}

    Matrix(const std::vector<std::vector<T>> &v){
        size_t m = v.size();
        assert(m != 0);
        size_t  n(v[0].size());
        for(int i(0); i < m; ++i)
            assert(v[i].size() == n);
        A = v;
    }

    Matrix(const std::vector<T> &v){
        size_t m = v.size();
        assert(m != 0);
        A.resize(1);
        A[0] = v;
    }

    ~Matrix() {}

    size_t height() const{return A.size();}

    size_t width() const{
        if(!height()) return 0;
        return A[0].size();
    }

    void setHeight(int h){A.resize(h, std::vector<T>(width(), 0));}

    void setWidth(int w){
        size_t m = height();
        for(int i(0); i < m; ++i) A[i].resize(w, 0);
    }

    void setSize(int h, int w){
        setHeight(h); setWidth(w);
    }

    void inputNoChangeSize(){
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                std::cin >> A[i][j];
    }

    void input(int h, int w){
        setSize(h, w);
        inputNoChangeSize();
    }

    void input(){
        int h, w; std::cin >> h >> w;
        input(h, w);
    }

    bool sameSize(const Matrix& mat) const{
        return height() == mat.height() && width() == mat.width();
    }

    inline const std::vector<T> &operator[](int idx) const{return A.at(idx);}

    inline std::vector<T> &operator[](int k){return (A.at(k));}

    void print2D(int w) const{
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i){
            for(int j(0); j < n; ++j){
                if(j) std::cout << " ";
                std::cout << std::setw(w) << (*this)[i][j];
            }
            std::cout << "\n";
        }
    }

    void print2D() const{
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i){
            for(int j(0); j < n; ++j){
                if(j) std::cout << " ";
                std::cout << (*this)[i][j];
            }
            std::cout << "\n";
        }
    }

    friend std::ostream& operator<<(std::ostream& os, const Matrix& B){
        size_t m = B.height(), n = B.width();
        for(int i(0); i < m; ++i){
            for(int j(0); j < n; ++j){
                if(j) os << " ";
                os << B[i][j];
            }
            os << "\n";
        }
        return (os);
    }

    static Matrix identity(size_t n){
        Matrix ret(n);
        for(int i(0); i < n; ++i) ret[i][i] = 1;
        return ret;
    }

    Matrix transpose() const{
        size_t n = height(), m = width();
        Matrix ret(m, n);
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                ret[i][j] = (*this)[j][i];
        return ret;
    }

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

    Matrix operator-() const{
        size_t m = height(), n = width();
        Matrix temp(height(), width());
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                temp[i][j] = -(*this)[i][j];
        return temp;
    }

    Matrix& operator=(const Matrix& B){
        if(this != &B){
            A = B.A;
        }
        return *this;
    }

    Matrix& operator+=(const Matrix& B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] += B[i][j];
        return *this;
    }

    Matrix& operator-=(const Matrix& B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] -= B[i][j];
        return *this;
    }

    Matrix& operator*=(const Matrix& B){
        size_t m = height(), n = width(), l = B.width();
        assert(n == B.height());
        std::vector<std::vector<T>> C(m, std::vector<T>(l, 0));
        for(int i(0); i < m; ++i)
            for(int j(0); j < l; ++j)
                for(int k(0); k < n; ++k)
                    C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
        A.swap(C);
        return *this;
    }

    Matrix& operator*=(const T a){
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] *= a;
        return *this;
    }

    Matrix& operator%=(const long long &mod){
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] %= mod;
        return *this;
    }

    Matrix& operator^=(const Matrix &B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] ^= B[i][j];
        return *this;
    }

    Matrix& operator|=(const Matrix &B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                (*this)[i][j] |= B[i][j];
        return *this;
    }

    Matrix& operator&=(const Matrix &B){
        size_t m = height(), n = width(), l = B.width();
        assert(n == B.height());
        std::vector<std::vector<T>> C(m, std::vector<T>(l, 0));
        for(int i(0); i < m; ++i)
            for(int j(0); j < l; ++j)
                for(int k(0); k < n; ++k)
                    C[i][j] = (C[i][j] ^ ((*this)[i][k] & B[k][j]));
        A.swap(C);
        return *this;
    }

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

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

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

    Matrix operator*(const T& a) const{
        Matrix temp(*this);
        return temp *= a;
    }

    friend Matrix operator*(T a, const Matrix& B){
        return B * a;
    }

    Matrix operator%(const long long mod) const{
        Matrix temp(*this);
        return temp %= mod;
    }

    Matrix operator^(const Matrix& mat) const{
        Matrix temp(*this);
        return temp ^= mat;
    }

    Matrix operator|(const Matrix& mat) const{
        Matrix temp(*this);
        return temp |= mat;
    }

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

    bool operator==(const Matrix& mat) const{
        if(!sameSize(mat)) return false;
        size_t m = height(), n = width();
        for(int i(0); i < m; ++i)
            for(int j(0); j < n; ++j)
                if((*this)[i][j] != mat[i][j]) return false;
        return true;
    }

    bool operator!=(const Matrix& mat) const{return !(*this == mat);}

    Matrix powWithoutMod(unsigned long long b) const{
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        Matrix a = *this;
        while(b){
            if(b & 1) ret = ret * a;
            a = a * a;
            b /= 2;
        }
        return ret;
    }

    Matrix power(unsigned long long b, long long mod) const{
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        Matrix a = *this;
        while(b){
            if(b & 1) ret = (ret * a) % mod;
            a = (a * a) % mod;
            for(int i(0); i < n; ++i){
                for(int j(0); j < n; ++j){
                    if(ret[i][j] < 0) ret[i][j] += mod;
                    if(a[i][j] < 0) a[i][j] += mod;
                }
            }
            b /= 2;
        }
        return ret;
    }

    Matrix power(unsigned long long b) const{
        return power(b, MOD);
    }

    Matrix powXorAnd(unsigned long long b) const{
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        ret *= -1;
        Matrix a = *this;
        while(b){
            if(b & 1) ret = ret & a;
            a = a & a;
            b /= 2;
        }
        return ret;
    }
};


int main(){
    int N; std::cin >> N;
    unsigned long long K; std::cin >> K;
    Matrix<long long> A(N);
    for(int i(0); i < N; ++i){
        for(int j(0); j < N; ++j){
            std::cin >> A[i][j];
        }
    }
    Matrix<long long> ansM(A.power(K));
    long long ans(0);
    for(int i(0); i < N; ++i){
        for(int j(0); j < N; ++j){
            ans = (ans + ansM[i][j]) % MOD;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}