n, m = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))

p = 10**9 + 7

def LCS(S,T,n,m):
  #初期化
  dp=[[0 for i in range(m+1)] for j in range(n+1)]
  for i in range(m+1): dp[i][0] = 1
  for j in range(n+1): dp[0][j] = 1
    
  #DP
  for i in range(n):
    for j in range(m):
      dp[i+1][j+1] = dp[i][j+1]%p + dp[i+1][j]%p
      if S[i] != T[j]:
        dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j]+1)%p
  return dp[n][m]%p

print(LCS(S,T,n,m))