import itertools
N, M = [int(_) for _ in input().split()]
S = [int(_) for _ in input().split()]
T = [int(_) for _ in input().split()]
mod = 10**9 + 7
dp = [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)]
for n, m in itertools.product(range(N), range(M)):
    dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m]
    if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
    dp[n + 1][m + 1] %= mod
print(dp[-1][-1]) import itertools itertools int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ N, M = [int(_) for _ in input().split()] N [int(_) for _ in input().split()] M int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ S = [int(_) for _ in input().split()] S [int(_) for _ in input().split()] int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ T = [int(_) for _ in input().split()] T [int(_) for _ in input().split()] mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 [1] + [0] * M for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [1] + [0] * M [1] 1 [0] * M [0] 0 M dp = [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)] dp [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)] [[1] * (M + 1)] [1] * (M + 1) [1] 1 M + 1 M 1 [[1] + [0] * M for _ in range(N)] for n, m in itertools.product(range(N), range(M)):
    dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m]
    if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
    dp[n + 1][m + 1] %= mod n m itertools.product(range(N), range(M)) itertools.product itertools product range(N) range N range(M) range M dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m] dp[n + 1][m + 1] [n + 1] dp n + 1 n 1 m + 1 m 1 dp[n][m + 1] + dp[n + 1][m] - dp[n][m] dp[n][m + 1] + dp[n + 1][m] dp[n][m + 1] [n] dp n m + 1 m 1 dp[n + 1][m] [n + 1] dp n + 1 n 1 m dp[n][m] [n] dp n m if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
     S[n] == T[m] S[n] S n T[m] T m dp[n + 1][m + 1] += dp[n][m] dp[n + 1][m + 1] [n + 1] dp n + 1 n 1 m + 1 m 1 dp[n][m] [n] dp n m dp[n + 1][m + 1] %= mod dp[n + 1][m + 1] [n + 1] dp n + 1 n 1 m + 1 m 1 mod print(dp[-1][-1]) print dp[-1][-1] [-1] dp -1 -1 mod = 10**9 + 7 10**9 + 7 mod M = [int(_) for _ in input().split()] [int(_) for _ in input().split()] M dp = [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)] [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)] dp T = [int(_) for _ in input().split()] [int(_) for _ in input().split()] T N, M = [int(_) for _ in input().split()] [int(_) for _ in input().split()] N S = [int(_) for _ in input().split()] [int(_) for _ in input().split()] S
import itertools
N, M = [int(_) for _ in input().split()]
S = [int(_) for _ in input().split()]
T = [int(_) for _ in input().split()]
mod = 10**9 + 7
dp = [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)]
for n, m in itertools.product(range(N), range(M)):
    dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m]
    if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
    dp[n + 1][m + 1] %= mod
print(dp[-1][-1])
