N, M = list(map(int, input().split()))

s = [int(c) for c in input().split()]

t = [int(c) for c in input().split()]

MOD = 10**9+7



dp = [[0]*(M+1) for i in range(N+1)]

dp_cum = [[0]*(M+1) for i in range(N+1)]



dp[0][0] = 1

for i in range(N+1):

  dp_cum[i][0] = 1

for j in range(M+1):

  dp_cum[0][j] = 1

  

for i in range(N):

  for j in range(M):

    if s[i]!=t[j]:

      dp[i+1][j+1] = 0

    else:

      dp[i+1][j+1] = dp_cum[i][j]

    dp_cum[i+1][j+1] = dp_cum[i][j+1] + dp_cum[i+1][j] - dp_cum[i][j] + dp[i+1][j+1]

    dp_cum[i+1][j+1] %= MOD



print((dp_cum[-1][-1]))
import numpy as np

MOD = 10**9+7

N, M = list(map(int, input().split()))

s = np.array(input().split(), dtype=np.int32)

t = np.array(input().split(), dtype=np.int32)



dp = np.zeros((N+1,M+1), dtype=np.int64)

dp[0] = 1



for i in range(N):

  same = (s[i]==t)

  dp[i+1,1:][same] = dp[i,:-1][same]

  dp[i+1] = (dp[i+1].cumsum()+dp[i])%MOD



print((dp[-1,-1]))

N, M = list(map(int, input().split()))
s = [int(c) for c in input().split()]
t = [int(c) for c in input().split()]
MOD = 10**9 + 7
dp = [[0] * (M + 1) for i in range(N + 1)]
dp_cum = [[0] * (M + 1) for i in range(N + 1)]
dp[0][0] = 1
for i in range(N + 1):
    dp_cum[i][0] = 1
for j in range(M + 1):
    dp_cum[0][j] = 1
for i in range(N):
    for j in range(M):
        if s[i] != t[j]:
            dp[i + 1][j + 1] = 0
        else:
            dp[i + 1][j + 1] = dp_cum[i][j]
        dp_cum[i + 1][j + 1] = (
            dp_cum[i][j + 1] + dp_cum[i + 1][j] - dp_cum[i][j] + dp[i + 1][j + 1]
        )
        dp_cum[i + 1][j + 1] %= MOD
print((dp_cum[-1][-1]))

import numpy as np

MOD = 10**9 + 7
N, M = list(map(int, input().split()))
s = np.array(input().split(), dtype=np.int32)
t = np.array(input().split(), dtype=np.int32)
dp = np.zeros((N + 1, M + 1), dtype=np.int64)
dp[0] = 1
for i in range(N):
    same = s[i] == t
    dp[i + 1, 1:][same] = dp[i, :-1][same]
    dp[i + 1] = (dp[i + 1].cumsum() + dp[i]) % MOD
print((dp[-1, -1]))

+import numpy as np + +MOD = 10**9 + 7 -s = [int(c) for c in input().split()] -t = [int(c) for c in input().split()] -MOD = 10**9 + 7 -dp = [[0] * (M + 1) for i in range(N + 1)] -dp_cum = [[0] * (M + 1) for i in range(N + 1)] -dp[0][0] = 1 -for i in range(N + 1): -    dp_cum[i][0] = 1 -for j in range(M + 1): -    dp_cum[0][j] = 1 +s = np.array(input().split(), dtype=np.int32) +t = np.array(input().split(), dtype=np.int32) +dp = np.zeros((N + 1, M + 1), dtype=np.int64) +dp[0] = 1 -    for j in range(M): -        if s[i] != t[j]: -            dp[i + 1][j + 1] = 0 -        else: -            dp[i + 1][j + 1] = dp_cum[i][j] -        dp_cum[i + 1][j + 1] = ( -            dp_cum[i][j + 1] + dp_cum[i + 1][j] - dp_cum[i][j] + dp[i + 1][j + 1] -        ) -        dp_cum[i + 1][j + 1] %= MOD -print((dp_cum[-1][-1])) +    same = s[i] == t +    dp[i + 1, 1:][same] = dp[i, :-1][same] +    dp[i + 1] = (dp[i + 1].cumsum() + dp[i]) % MOD +print((dp[-1, -1]))