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

res = [[-1 for _ in range(M)] for _ in range(N)]
dpl = [[-1 for _ in range(M)] for _ in range(N)]
def dp(i, j):
  if i < 0:
    return 0
  if j < 0:
    return 0
  if dpl[i][j] != -1:
    return dpl[i][j]
  if i == 1:
    temp = 0
    for k in range(j):
      temp += S[i] == T[k]
    return temp
  elif j == 1:
    temp = 0
    for k in range(j):
      temp += S[k] == T[j]
    return temp
  temp = 0
  for k in range(i):
    for l in range(j):
      temp += dpl[k][l]
      
  return temp

def search(i, j):
  if i < 0:
    return 0
  if j < 0:
    return 0
  if res[i][j] != -1:
    return res[i][j]
  temp = 0
  if S[i] == T[j]:
    temp = 1
    temp += dp[i][j]
  else:
    temp = 0
  return search(i - 1, j) + search(i, j - 1) - search(i - 1, j - 1) + temp

print(search(N - 1, M - 1) % mod)
