Travel around China
Time limit8sMemory limit512 MB
Given a 3 by m grid of city costs, sum the minimum route cost over all ordered pairs of distinct cities, where each visit to a city adds its cost, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Shortest path, Matrix
- Solved
- No attempts yet
Problem
Rikka is a rich girl.
She would like to visit the beautiful cities of China. The city locations in China can be simply regarded as a grid with rows and columns. Rows are numbered from to from north to south, and columns are numbered from to from west to east. The city in row and column is called .
City has a direct expressway to city if and only if . Because New Year is coming, the expressways are open to the public free of charge.
When Rikka travels from city to , she can only use expressways. The cost of a route is the sum of the costs of all cities she visits, including the starting and ending cities. If the route includes city , she spends yuan. If she visits city a total of times, she spends yuan, because there are always enough shopping malls for her to spend money.
Rikka is a fanciful girl, and she does not even pick a starting and ending city. Let be the minimal cost of a route from city to city . She wants the value
Since the answer may be very large, output it modulo .
Input
The first line contains two integers and .
Each of the following lines contains integers. The -th number in the -th line is .
It is guaranteed that , , and .
Output
Output a single integer, the answer modulo .