import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7
N, M = lr()
S = ['?'] + lr()
T = ['.'] +lr()
# dp[i][j] はSがi文字目までTがj文字目までの部分列で等しい組の総和
dp = [[1] * (M+1) for _ in range(N+1)]

for j in range(1, M+1):
    for i in range(1, N+1):
        dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
        if S[i] == T[j]:
            dp[i][j] += (dp[i-1][j-1])
        dp[i][j] %= MOD

answer = dp[N][M]
print(answer%MOD)
# 21 pypy import sys sys sr = lambda: sys.stdin.readline().rstrip() sr lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip ir = lambda: int(sr()) ir lambda: int(sr()) int(sr()) int sr() sr lr = lambda: list(map(int, sr().split())) lr lambda: list(map(int, sr().split())) list(map(int, sr().split())) list map(int, sr().split()) map int sr().split() ().split () sr split MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 N, M = lr() N lr() lr M S = ['?'] + lr() S ['?'] + lr() ['?'] '?' lr() lr T = ['.'] +lr() T ['.'] +lr() ['.'] '.' lr() lr [1] * (M+1) for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [1] * (M+1) [1] 1 M+1 M 1 dp = [[1] * (M+1) for _ in range(N+1)] dp [[1] * (M+1) for _ in range(N+1)] for j in range(1, M+1):
    for i in range(1, N+1):
        dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
        if S[i] == T[j]:
            dp[i][j] += (dp[i-1][j-1])
        dp[i][j] %= MOD j range(1, M+1) range 1 M+1 M 1 for i in range(1, N+1):
        dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
        if S[i] == T[j]:
            dp[i][j] += (dp[i-1][j-1])
        dp[i][j] %= MOD i range(1, N+1) range 1 N+1 N 1 dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] dp[i][j] [i] dp i j dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] dp[i-1][j] + dp[i][j-1] dp[i-1][j] [i-1] dp i-1 i 1 j dp[i][j-1] [i] dp i j-1 j 1 dp[i-1][j-1] [i-1] dp i-1 i 1 j-1 j 1 if S[i] == T[j]:
            dp[i][j] += (dp[i-1][j-1])
         S[i] == T[j] S[i] S i T[j] T j dp[i][j] += (dp[i-1][j-1]) dp[i][j] [i] dp i j dp[i-1][j-1] [i-1] dp i-1 i 1 j-1 j 1 dp[i][j] %= MOD dp[i][j] [i] dp i j MOD answer = dp[N][M] answer dp[N][M] [N] dp N M print(answer%MOD) print answer%MOD answer MOD N, M = lr() lr() N dp = [[1] * (M+1) for _ in range(N+1)] [[1] * (M+1) for _ in range(N+1)] dp sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr answer = dp[N][M] dp[N][M] answer lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr ir = lambda: int(sr()) lambda: int(sr()) ir S = ['?'] + lr() ['?'] + lr() S MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD M = lr() lr() M T = ['.'] +lr() ['.'] +lr() T
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7
N, M = lr()
S = ['?'] + lr()
T = ['.'] +lr()
# dp[i][j] はSがi文字目までTがj文字目までの部分列で等しい組の総和
dp = [[1] * (M+1) for _ in range(N+1)]

for j in range(1, M+1):
    for i in range(1, N+1):
        dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
        if S[i] == T[j]:
            dp[i][j] += (dp[i-1][j-1])
        dp[i][j] %= MOD

answer = dp[N][M]
print(answer%MOD)
# 21 pypy