Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created April 13, 2024 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/11055dd34d7a4cb3d9754e3fa5103587 to your computer and use it in GitHub Desktop.
Save maehrm/11055dd34d7a4cb3d9754e3fa5103587 to your computer and use it in GitHub Desktop.
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