n,m = map(int,input().split())
S = ['a']+list(input().split())
T = ['a']+list(input().split())
dp = [[1]*(n+1) for i in range(m+1)]
mod = 10**9+7
#dp[i][j]:sのi番までとtのj番目まででの共通部分列の個数
for i in range(1,n+1):
    for j in range(1,m+1):
        if S[i]==T[j]:
            dp[i][j] = dp[i-1][j]+dp[i][j-1]
        else:
            dp[i][j] = max(dp[i-1][j],dp[i][j-1])
        dp[i][j]%=mod
print(dp[-1][-1])