# ほぼ解説AC
# n,m<2000
# len(s),len(t)<100000
import sys

input = sys.stdin.buffer.readline

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


dp = [[0] * (m + 2) for _ in range(n + 2)]
sum_dp = [[0] * (m + 2) for _ in range(n + 2)]
# 空集合も要素とみなすのでdp[0][0]は1
dp[0][0] = 0
# i,jを見る時にi,jまでの累積和がほすぃ。
# i,jを見てi+1,j+1を決める。
for i in range(n + 1):
    for j in range(m + 1):
        # 二次元累積和の計算
        sum_dp[i][j] += dp[i][j]
        if i - 1 >= 0:
            sum_dp[i][j] += sum_dp[i - 1][j]
        if j - 1 >= 0:
            sum_dp[i][j] += sum_dp[i][j - 1]
        if i - 1 >= 0 and j - 1 >= 0:
            sum_dp[i][j] -= sum_dp[i - 1][j - 1]
        sum_dp[i][j] %= mod
        if i < n and j < m:
            # 先にsum_dp[i][j]は計算されていないとだめ。↑
            # i-1字目、j-1字目まででできるものに、新たに一致したものを加えたもの(sum_dp)と空集合(+1)
            if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod
print((sum_dp[n][m] + 1) % mod)
 import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 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 [0] * (m + 2) for _ in range(n + 2) for _ in range(n + 2) _ range(n + 2) range n + 2 n 2 for _ in range(n + 2) [0] * (m + 2) [0] 0 m + 2 m 2 dp = [[0] * (m + 2) for _ in range(n + 2)] dp [[0] * (m + 2) for _ in range(n + 2)] [0] * (m + 2) for _ in range(n + 2) for _ in range(n + 2) _ range(n + 2) range n + 2 n 2 for _ in range(n + 2) [0] * (m + 2) [0] 0 m + 2 m 2 sum_dp = [[0] * (m + 2) for _ in range(n + 2)] sum_dp [[0] * (m + 2) for _ in range(n + 2)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(n + 1):
    for j in range(m + 1):
        # 二次元累積和の計算
        sum_dp[i][j] += dp[i][j]
        if i - 1 >= 0:
            sum_dp[i][j] += sum_dp[i - 1][j]
        if j - 1 >= 0:
            sum_dp[i][j] += sum_dp[i][j - 1]
        if i - 1 >= 0 and j - 1 >= 0:
            sum_dp[i][j] -= sum_dp[i - 1][j - 1]
        sum_dp[i][j] %= mod
        if i < n and j < m:
            # 先にsum_dp[i][j]は計算されていないとだめ。↑
            # i-1字目、j-1字目まででできるものに、新たに一致したものを加えたもの(sum_dp)と空集合(+1)
            if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod i range(n + 1) range n + 1 n 1 for j in range(m + 1):
        # 二次元累積和の計算
        sum_dp[i][j] += dp[i][j]
        if i - 1 >= 0:
            sum_dp[i][j] += sum_dp[i - 1][j]
        if j - 1 >= 0:
            sum_dp[i][j] += sum_dp[i][j - 1]
        if i - 1 >= 0 and j - 1 >= 0:
            sum_dp[i][j] -= sum_dp[i - 1][j - 1]
        sum_dp[i][j] %= mod
        if i < n and j < m:
            # 先にsum_dp[i][j]は計算されていないとだめ。↑
            # i-1字目、j-1字目まででできるものに、新たに一致したものを加えたもの(sum_dp)と空集合(+1)
            if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod j range(m + 1) range m + 1 m 1 sum_dp[i][j] += dp[i][j] sum_dp[i][j] [i] sum_dp i j dp[i][j] [i] dp i j if i - 1 >= 0:
            sum_dp[i][j] += sum_dp[i - 1][j]
         i - 1 >= 0 i - 1 i 1 0 sum_dp[i][j] += sum_dp[i - 1][j] sum_dp[i][j] [i] sum_dp i j sum_dp[i - 1][j] [i - 1] sum_dp i - 1 i 1 j if j - 1 >= 0:
            sum_dp[i][j] += sum_dp[i][j - 1]
         j - 1 >= 0 j - 1 j 1 0 sum_dp[i][j] += sum_dp[i][j - 1] sum_dp[i][j] [i] sum_dp i j sum_dp[i][j - 1] [i] sum_dp i j - 1 j 1 if i - 1 >= 0 and j - 1 >= 0:
            sum_dp[i][j] -= sum_dp[i - 1][j - 1]
         i - 1 >= 0 and j - 1 >= 0 i - 1 >= 0 i - 1 i 1 0 j - 1 >= 0 j - 1 j 1 0 sum_dp[i][j] -= sum_dp[i - 1][j - 1] sum_dp[i][j] [i] sum_dp i j sum_dp[i - 1][j - 1] [i - 1] sum_dp i - 1 i 1 j - 1 j 1 sum_dp[i][j] %= mod sum_dp[i][j] [i] sum_dp i j mod if i < n and j < m:
            # 先にsum_dp[i][j]は計算されていないとだめ。↑
            # i-1字目、j-1字目まででできるものに、新たに一致したものを加えたもの(sum_dp)と空集合(+1)
            if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod i < n and j < m i < n i n j < m j m if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod S[i] == T[j] S[i] S i T[j] T j dp[i + 1][j + 1] = sum_dp[i][j] + 1 dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 sum_dp[i][j] + 1 sum_dp[i][j] [i] sum_dp i j 1 dp[i + 1][j + 1] %= mod dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 mod print((sum_dp[n][m] + 1) % mod) print (sum_dp[n][m] + 1) % mod sum_dp[n][m] + 1 sum_dp[n][m] [n] sum_dp n m 1 mod input = sys.stdin.buffer.readline sys.stdin.buffer.readline input m = map(int, input().split()) map(int, input().split()) m dp = [[0] * (m + 2) for _ in range(n + 2)] [[0] * (m + 2) for _ in range(n + 2)] dp n, m = map(int, input().split()) map(int, input().split()) n S = list(map(int, input().split())) list(map(int, input().split())) S mod = 10 ** 9 + 7 10 ** 9 + 7 mod sum_dp = [[0] * (m + 2) for _ in range(n + 2)] [[0] * (m + 2) for _ in range(n + 2)] sum_dp T = list(map(int, input().split())) list(map(int, input().split())) T
# ほぼ解説AC
# n,m<2000
# len(s),len(t)<100000
import sys

input = sys.stdin.buffer.readline

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


dp = [[0] * (m + 2) for _ in range(n + 2)]
sum_dp = [[0] * (m + 2) for _ in range(n + 2)]
# 空集合も要素とみなすのでdp[0][0]は1
dp[0][0] = 0
# i,jを見る時にi,jまでの累積和がほすぃ。
# i,jを見てi+1,j+1を決める。
for i in range(n + 1):
    for j in range(m + 1):
        # 二次元累積和の計算
        sum_dp[i][j] += dp[i][j]
        if i - 1 >= 0:
            sum_dp[i][j] += sum_dp[i - 1][j]
        if j - 1 >= 0:
            sum_dp[i][j] += sum_dp[i][j - 1]
        if i - 1 >= 0 and j - 1 >= 0:
            sum_dp[i][j] -= sum_dp[i - 1][j - 1]
        sum_dp[i][j] %= mod
        if i < n and j < m:
            # 先にsum_dp[i][j]は計算されていないとだめ。↑
            # i-1字目、j-1字目まででできるものに、新たに一致したものを加えたもの(sum_dp)と空集合(+1)
            if S[i] == T[j]:
                dp[i + 1][j + 1] = sum_dp[i][j] + 1
                dp[i + 1][j + 1] %= mod
print((sum_dp[n][m] + 1) % mod)

