import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = 10**9 + 7

memo = [[0] * M for _ in range(N)]
def dfs(i, j):
    if i < 0 or j < 0:
        return 1
    if memo[i][j] != 0:
        return memo[i][j]
    if S[i] == T[j]:
        memo[i][j] = (dfs(i-1, j) + dfs(i, j-1)) % mod
    else:
        memo[i][j] = (dfs(i-1, j) + dfs(i, j-1) - dfs(i-1, j-1)) % mod
    return memo[i][j]

print(dfs(N-1, M-1))