import sys

sys.setrecursionlimit(1000000000)
mod = 10**9+7
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
memo = [[-1]*(M+1) for _ in range(N)]
List = [[]for _ in range(N)]

for i in range(N):
    for j in range(M):
        if S[i]==T[j]:
            List[i].append(j)
def dp(i, j):
    if i==N-1:
        cnt = 0
        for k in List[i]:
            if j<k:
                cnt+=1
        return (cnt+1)%mod
    res = 0
    for k in List[i]:
        if j<k:
            if memo[i+1][k]<0:
                memo[i+1][k]=dp(i+1, k)
            res+=memo[i+1][k]
    if memo[i+1][j]<0:
        memo[i+1][j]=dp(i+1, j)
    res+=memo[i+1][j]
    return res%mod
print(dp(0, -1)%mod)
