# 両方の数列から同じ部分列がいくつあるか？

MOD = 10**9+7

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

S = [0] + list(map(int, input().split()))

T = [0] + list(map(int, input().split()))

# 最後に(n,m)でとる

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

# 2次元累積和、(n,m) inclusive. 

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



# empty

dp[0][0] = 1

dp_cum[0][0] = 1

for n in range(N+1):

  dp_cum[n][0] = 1

for m in range(M+1):

  dp_cum[0][m] = 1  



for n in range(1, N+1):

  for m in range(1, M+1):

    if S[n] != T[m]:

      dp[n][m] = 0

    else:

      # (n-1, m-1)までで作れてるやつから飛んでくる

      dp[n][m] = dp_cum[n-1][m-1]

    dp_cum[n][m] = dp_cum[n-1][m] + dp_cum[n][m-1] - dp_cum[n-1][m-1] + dp[n][m]

    dp_cum[n][m] %= MOD



answer = dp_cum[N][M]

print(answer)    

      

    

  
# 両方の数列から同じ部分列がいくつあるか？

import numpy as np



MOD = 10**9+7

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

S = input().split()

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

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



for s in S:

  dp[1:] = ((dp[:-1] * (T == int(s))).cumsum() + dp[1:]) % MOD



print((dp[M]))



# 両方の数列から同じ部分列がいくつあるか？
MOD = 10**9 + 7
N, M = list(map(int, input().split()))
S = [0] + list(map(int, input().split()))
T = [0] + list(map(int, input().split()))
# 最後に(n,m)でとる
dp = [[0] * (M + 1) for _ in range(N + 1)]
# 2次元累積和、(n,m) inclusive.
dp_cum = [[0] * (M + 1) for _ in range(N + 1)]
# empty
dp[0][0] = 1
dp_cum[0][0] = 1
for n in range(N + 1):
    dp_cum[n][0] = 1
for m in range(M + 1):
    dp_cum[0][m] = 1
for n in range(1, N + 1):
    for m in range(1, M + 1):
        if S[n] != T[m]:
            dp[n][m] = 0
        else:
            # (n-1, m-1)までで作れてるやつから飛んでくる
            dp[n][m] = dp_cum[n - 1][m - 1]
        dp_cum[n][m] = (
            dp_cum[n - 1][m] + dp_cum[n][m - 1] - dp_cum[n - 1][m - 1] + dp[n][m]
        )
        dp_cum[n][m] %= MOD
answer = dp_cum[N][M]
print(answer)

# 両方の数列から同じ部分列がいくつあるか？
import numpy as np

MOD = 10**9 + 7
N, M = list(map(int, input().split()))
S = input().split()
T = np.array(input().split(), dtype=np.int32)
dp = np.ones(M + 1, dtype=np.int64)
for s in S:
    dp[1:] = ((dp[:-1] * (T == int(s))).cumsum() + dp[1:]) % MOD
print((dp[M]))

+import numpy as np + -S = [0] + list(map(int, input().split())) -T = [0] + list(map(int, input().split())) -# 最後に(n,m)でとる -dp = [[0] * (M + 1) for _ in range(N + 1)] -# 2次元累積和、(n,m) inclusive. -dp_cum = [[0] * (M + 1) for _ in range(N + 1)] -# empty -dp[0][0] = 1 -dp_cum[0][0] = 1 -for n in range(N + 1): -    dp_cum[n][0] = 1 -for m in range(M + 1): -    dp_cum[0][m] = 1 -for n in range(1, N + 1): -    for m in range(1, M + 1): -        if S[n] != T[m]: -            dp[n][m] = 0 -        else: -            # (n-1, m-1)までで作れてるやつから飛んでくる -            dp[n][m] = dp_cum[n - 1][m - 1] -        dp_cum[n][m] = ( -            dp_cum[n - 1][m] + dp_cum[n][m - 1] - dp_cum[n - 1][m - 1] + dp[n][m] -        ) -        dp_cum[n][m] %= MOD -answer = dp_cum[N][M] -print(answer) +S = input().split() +T = np.array(input().split(), dtype=np.int32) +dp = np.ones(M + 1, dtype=np.int64) +for s in S: +    dp[1:] = ((dp[:-1] * (T == int(s))).cumsum() + dp[1:]) % MOD +print((dp[M]))