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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace Contest
{

    class Program
    {
        private const long Mod = (long)1e9 + 7;
        private Scanner sc;
        private int N;
        private long K;
        private List<int>[] E;

        private bool[,] A;
        // 長さ2^i start j end k DPLog[i,j,k]
        private long[,,] DPLog;

        public void Solve()
        {
            sc = new Scanner();
            N = sc.NextInt();
            K = sc.NextLong();
            E = new List<int>[N];
            A = new bool[N, N];
            for (int i = 0; i < N; i++)
            {
                E[i] = new List<int>();
            }
            for (int i = 0; i < N; i++)
            {
                var ar = sc.Array();
                for (int j = 0; j < N; j++)
                {
                    if (ar[j] == "1")
                        E[i].Add(j);
                    A[i, j] = ar[j] == "1";
                }
            }

            DPLog = new long[64, N, N];

            for (int i = 0; i < 64; i++)
            {
                for (int start = 0; start < N; start++)
                {
                    if (i == 0)
                    {
                        foreach (var i1 in E[start])
                        {
                            DPLog[i, start, i1] = 1;
                        }
                    }
                    else
                    {
                        for (int end = 0; end < N; end++)
                        {
                            for (int j = 0; j < N; j++)
                            {
                                DPLog[i, start, end] += (DPLog[i - 1, start, j] * DPLog[i - 1, j, end]) % Mod;
                                DPLog[i, start, end] %= Mod;
                            }
                        }
                    }
                }
            }


            //for (int j = 0; j < N; j++)
            //{
            //    for (int k = 0; k < N; k++)
            //    {
            //        Console.Write(DPLog[1, j, k]);
            //        Console.Write(" ");
            //    }
            //    Console.WriteLine();

            //}

            var dp = new long[N, N];
            for (int i = 0; i < N; i++)
            {
                dp[i, i] = 1;
            }
            long b = 0;
            while (K > 0)
            {
                if (K % 2 == 1)
                {
                    var next = new long[N, N];
                    for (int start = 0; start < N; start++)
                    {
                        for (int end = 0; end < N; end++)
                        {
                            for (int i = 0; i < N; i++)
                            {
                                next[start, end] += (dp[start, i] * DPLog[b, i, end]) % Mod;
                                next[start, end] %= Mod;
                            }
                        }
                    }

                    dp = next;
                }

                b++;
                K /= 2;
            }

            long ans = 0;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    ans += dp[i, j];
                    ans %= Mod;
                }
            }
            Console.WriteLine(ans);
        }


        static void Main() => new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _stream = new StreamReader(Console.OpenStandardInput());
        _pos = 0;
        _line = new string[0];
        _separator = ' ';
    }
    private readonly char _separator;
    private readonly StreamReader _stream;
    private int _pos;
    private string[] _line;
    #region get a element
    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = _stream.ReadLine().Split(_separator);
            _pos = 0;
        }
        return _line[_pos++];
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    #endregion
    #region convert array
    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }
    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }
    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }
    #endregion
    #region get array
    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = _stream.ReadLine().Split(_separator);
        _pos = _line.Length;
        return _line;
    }
    public int[] IntArray()
    {
        return ToIntArray(Array());
    }
    public long[] LongArray()
    {
        return ToLongArray(Array());
    }
    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }
    #endregion
}