Danger at the Marks
View as PDFEric and Lucas are both training for the Olympic sailing regatta, but the venue has only one course. Neither of them is willing to compromise or take turns, so they have to practise in the same space. The danger is at the marks (the inflatable buoys sailors must round), where boats bunch up and can collide. Coach Hung wants them to meet as rarely as possible. Eric always launches from the same mark, while Lucas may start at any other mark, and you have been asked how often they should expect to meet in each case.
The course is laid out around marks, numbered from
to
. Wind and currents
push sailors between these marks according to fixed patterns.
For every pair of marks and
, you are given an integer
. At the end of
each minute, a sailor currently at mark
moves to mark
with probability
. Every row of these values sums to
.
Eric and Lucas follow these probabilities independently. Eric always starts at mark
. Lucas may start at any mark
except mark
.
Whenever both of them occupy the same mark after a minute has passed, they are said to have met.

For every starting mark of Lucas from
to
, determine the expected number of
times the two sailors meet during the next
minutes.
Because the answers may be fractional, output them modulo . If an expected value
equals the rational number
in lowest terms, output
.
Input
The first line contains two integers and
(
,
).
The next lines each contain
integers. The
-th integer on the
-th of these
lines is
(
). For every row
,
.
Output
Output a single line containing integers. The
-th of these should be the
required answer when Lucas starts at mark
.
Example 1
Input
2 2
80 20
30 70
Output
20000001
Explanation
The expected value is .
Example 2
Input
4 5
40 10 20 30
0 50 50 0
25 25 25 25
10 20 30 40
Output
21336070 454089224 404025250
Comments