from collections import defaultdict
import bisect
import numpy as np

MOD = 10 ** 9 + 7

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

S_appeared_indices = defaultdict(list)
for i, c1 in enumerate(S):
    S_appeared_indices[c1].append(i)

T_appeared_indices = defaultdict(list)
for j, c2 in enumerate(T):
    T_appeared_indices[c2].append(j)

dp = np.zeros((N+1, M+1), np.int64)
dp[0, :] = 1
dp[:, 0] = 1

for i, c1 in enumerate(S):
    for j, c2 in enumerate(T):
        dp[i+1, j+1] = dp[i+1, j]
        if c2 in S_appeared_indices:
            tmp = bisect.bisect_right(S_appeared_indices[c2], i) - 1
            while tmp >= 0:
                S_index = S_appeared_indices[c2][tmp]
                dp[i+1, j+1] += dp[S_index, j]
                tmp -= 1
        dp[i+1, j+1] %= MOD

print(dp[N, M])

    

