Created
April 13, 2024 00:44
-
-
Save maehrm/11055dd34d7a4cb3d9754e3fa5103587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.setrecursionlimit(10**7) | |
dir = [(1, 0), (0, 1), (-1, 0), (0, -1)] | |
MOD = 10**9 + 7 | |
def dfs(x, y): | |
if dp[y][x] != 0: | |
return dp[y][x] | |
if dp[y][x] == 0: | |
dp[y][x] = 1 | |
for dx, dy in dir: | |
nxt_x = x + dx | |
nxt_y = y + dy | |
if nxt_x < 0 or nxt_y < 0 or nxt_x >= W or nxt_y >= H: | |
continue | |
if a[nxt_y][nxt_x] <= a[y][x]: | |
continue | |
dp[y][x] += dfs(nxt_x, nxt_y) | |
dp[y][x] %= MOD | |
return dp[y][x] | |
H, W = map(int, input().split()) | |
a = [list(map(int, input().split())) for _ in range(H)] | |
dp = [[0] * W for _ in range(H)] | |
for y in range(H): | |
for x in range(W): | |
dfs(x, y) | |
ans = 0 | |
for i in range(H): | |
ans += sum(dp[i]) | |
print(ans % MOD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment