33OJ
登录 注册

#P17701. [ICPC 2019 Nanjing R] Digital Path

    ID: 20153 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>动态规划 DP图论2019ICPC南京

[ICPC 2019 Nanjing R] Digital Path

Problem Description

Zhe the bully, is condemned by all kinds of evil, like bullying those who are weaker. His teammates have been mistreated for a long time. Finally, they decided not to put up with their buddy any more and flee to Digital Village, with the bully in hot pursuit. Due to difficult terrain and a considerable amount of Digital Paths staggered, they can't be easily arrested.

Getting familiar with the terrain as soon as possible is important for these innocents to escape the threat of bullying. All they need now is to count the number of Digital Paths in Digital Village.

To simplify the problem, Digital Village is abstracted into a grid with nn rows and mm columns filled by integers. A Digital Path is a continuous walk in the grid satisfying the following conditions:

  • adjacent boxes in the walk share a common edge;
  • the walk is maximal, which cannot be extended;
  • the walk contains at least four boxes;
  • going from one end to the other, the increment of values for any two adjacent boxes is exactly one.

Here we have some examples.

:::align{center} :::

The path in Figure 1 is invalid because its length is less than 44.

:::align{center} :::

The path in Figure 2 is invalid because it is not continuous.

:::align{center} ::: The path in Figure 3 is invalid because it can be extended further.

:::align{center} :::

The path in Figure 4 is also invalid because values in the path are not strictly increased by one.

:::align{center} :::

Digital Paths may partially overlap. In Figure 5, there are 44 Digital Paths marked by different colours.

Input Format

The first line contains two positive integers nn and mm (1≤n,m≤10001\le n,m\le 1000) describing the size of the grid.

Each of the next nn lines contains mm integers, the jj-th of which, denoted by ai,ja_{i,j} (−107≤ai,j≤107-10^7\le a_{i,j}\le 10^7), represents the value of the box in the ii-th row and the jj-th column.

Output Format

Output the number of Digital Paths modulo (109+7)(10^9+7).

3 5
1 2 3 8 7
-1 -1 4 5 6
1 2 3 8 7
4
4 4
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
16