# numpyでかいてもTLEした。
# pypyでlistでかくととおる？

#import numpy as np
N,M = list(map(int, input().split()))
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = int(1e9+7)
"""
dp[i][j] : S の i 文字目までと T の j 文字目までを考慮し、
この 2 文字 (S[i] と T[j]）をペアにするときの場合の数とすると、

dp[i][j] = (sum(dp[1:i-1][1:j-1]) + 1 if S[i] == T[j] else 0)
このsumの部分をどうもとめるか。

最終的な答えはsum(dp[:][:])。

2-D cumsumをつかう
i,j = 0のときにきをつけて
DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
"""

dp = [[ 0 for _ in range(M)] for _ in range(N)]
DS = [[ 0 for _ in range(M+1)] for _ in range(N+1)]

for i in range(N):
  for j in range(M):
    if S[i] != T[j]:
      dp[i][j] = 0
    else:
      dp[i][j] += 1
      dp[i][j] += DS[i-1][j-1]
      dp[i][j] %= mod
    DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
    DS[i][j] %= mod
print(DS[-2][-2]+1)
#print(dp)
#print(DS) N,M = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split M S = list(map(int, input().split())) S list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split T = list(map(int, input().split())) T list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split mod = int(1e9+7) mod int(1e9+7) int 1e9+7 1e9 7 """
dp[i][j] : S の i 文字目までと T の j 文字目までを考慮し、
この 2 文字 (S[i] と T[j]）をペアにするときの場合の数とすると、

dp[i][j] = (sum(dp[1:i-1][1:j-1]) + 1 if S[i] == T[j] else 0)
このsumの部分をどうもとめるか。

最終的な答えはsum(dp[:][:])。

2-D cumsumをつかう
i,j = 0のときにきをつけて
DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
""" [ 0 for _ in range(M)] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [ 0 for _ in range(M)] dp = [[ 0 for _ in range(M)] for _ in range(N)] dp [[ 0 for _ in range(M)] for _ in range(N)] [ 0 for _ in range(M+1)] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [ 0 for _ in range(M+1)] DS = [[ 0 for _ in range(M+1)] for _ in range(N+1)] DS [[ 0 for _ in range(M+1)] for _ in range(N+1)] for i in range(N):
  for j in range(M):
    if S[i] != T[j]:
      dp[i][j] = 0
    else:
      dp[i][j] += 1
      dp[i][j] += DS[i-1][j-1]
      dp[i][j] %= mod
    DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
    DS[i][j] %= mod i range(N) range N for j in range(M):
    if S[i] != T[j]:
      dp[i][j] = 0
    else:
      dp[i][j] += 1
      dp[i][j] += DS[i-1][j-1]
      dp[i][j] %= mod
    DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
    DS[i][j] %= mod j range(M) range M if S[i] != T[j]:
      dp[i][j] = 0
    else:
      dp[i][j] += 1
      dp[i][j] += DS[i-1][j-1]
      dp[i][j] %= mod
     S[i] != T[j] S[i] S i T[j] T j dp[i][j] = 0 dp[i][j] [i] dp i j 0 dp[i][j] += 1 dp[i][j] [i] dp i j 1 dp[i][j] += DS[i-1][j-1] dp[i][j] [i] dp i j DS[i-1][j-1] [i-1] DS i-1 i 1 j-1 j 1 dp[i][j] %= mod dp[i][j] [i] dp i j mod DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j] DS[i][j] [i] DS i j DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j] DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] DS[i-1][j] + DS[i][j-1] DS[i-1][j] [i-1] DS i-1 i 1 j DS[i][j-1] [i] DS i j-1 j 1 DS[i-1][j-1] [i-1] DS i-1 i 1 j-1 j 1 dp[i][j] [i] dp i j DS[i][j] %= mod DS[i][j] [i] DS i j mod print(DS[-2][-2]+1) print DS[-2][-2]+1 DS[-2][-2] [-2] DS -2 -2 1 mod = int(1e9+7) int(1e9+7) mod S = list(map(int, input().split())) list(map(int, input().split())) S DS = [[ 0 for _ in range(M+1)] for _ in range(N+1)] [[ 0 for _ in range(M+1)] for _ in range(N+1)] DS M = list(map(int, input().split())) list(map(int, input().split())) M T = list(map(int, input().split())) list(map(int, input().split())) T N,M = list(map(int, input().split())) list(map(int, input().split())) N dp = [[ 0 for _ in range(M)] for _ in range(N)] [[ 0 for _ in range(M)] for _ in range(N)] dp
# numpyでかいてもTLEした。
# pypyでlistでかくととおる？

#import numpy as np
N,M = list(map(int, input().split()))
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = int(1e9+7)
"""
dp[i][j] : S の i 文字目までと T の j 文字目までを考慮し、
この 2 文字 (S[i] と T[j]）をペアにするときの場合の数とすると、

dp[i][j] = (sum(dp[1:i-1][1:j-1]) + 1 if S[i] == T[j] else 0)
このsumの部分をどうもとめるか。

最終的な答えはsum(dp[:][:])。

2-D cumsumをつかう
i,j = 0のときにきをつけて
DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
"""

dp = [[ 0 for _ in range(M)] for _ in range(N)]
DS = [[ 0 for _ in range(M+1)] for _ in range(N+1)]

for i in range(N):
  for j in range(M):
    if S[i] != T[j]:
      dp[i][j] = 0
    else:
      dp[i][j] += 1
      dp[i][j] += DS[i-1][j-1]
      dp[i][j] %= mod
    DS[i][j] = DS[i-1][j] + DS[i][j-1] - DS[i-1][j-1] + dp[i][j]
    DS[i][j] %= mod
print(DS[-2][-2]+1)
#print(dp)
#print(DS)
