#dpr. [dp_r]Walk
[dp_r]Walk
Problem Statement
There is a simple directed graph with vertices, numbered .
For each and (), you are given an integer that represents whether there is a directed edge from Vertex to . If , there is a directed edge from Vertex to ; if , there is not.
Find the number of different directed paths of length in , modulo . We will also count a path that traverses the same edge multiple times.
Constraints
- All values in input are integers.
- is or .
Input
Input is given from Standard Input in the following format:
Output
Print the number of different directed paths of length in , modulo .
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
is drawn in the figure below:
There are six directed paths of length :
- → →
- → →
- → →
- → →
- → →
- → →
Sample Input 2
3 3
0 1 0
1 0 1
0 0 0
Sample Output 2
3
is drawn in the figure below:
There are three directed paths of length :
- → → →
- → → →
- → → →
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
is drawn in the figure below:
There is one directed path of length :
- → →
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 .