# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

# dp[i][j] : Sの左からi文字目までと Tの左からj文字目まででの 共通部分文字列の数
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
dp[0][0] = 1

for i in range(N+1):
    for j in range(M+1):
        if 0<=i-1 and 0<=j-1 and S[i-1]==T[j-1]:
            dp[i][j] += dp[i-1][j-1]
        if 0<=i-1:
            dp[i][j] += dp[i-1][j]
        if 0<=j-1:
            dp[i][j] += dp[i][j-1]
        # 重複カウント部分を減算する
        if 0<=i-1 and 0<=j-1:
            dp[i][j] -= dp[i-1][j-1]
        dp[i][j] %= mod

print(dp[N][M]) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N,M = map(int,input().split()) N 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 = 10**9+7 mod 10**9+7 10**9 10 9 7 [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)] dp = [[0 for _ in range(M+1)] for _ in range(N+1)] dp [[0 for _ in range(M+1)] for _ in range(N+1)] dp[0][0] = 1 dp[0][0] [0] dp 0 0 1 for i in range(N+1):
    for j in range(M+1):
        if 0<=i-1 and 0<=j-1 and S[i-1]==T[j-1]:
            dp[i][j] += dp[i-1][j-1]
        if 0<=i-1:
            dp[i][j] += dp[i-1][j]
        if 0<=j-1:
            dp[i][j] += dp[i][j-1]
        # 重複カウント部分を減算する
        if 0<=i-1 and 0<=j-1:
            dp[i][j] -= dp[i-1][j-1]
        dp[i][j] %= mod i range(N+1) range N+1 N 1 for j in range(M+1):
        if 0<=i-1 and 0<=j-1 and S[i-1]==T[j-1]:
            dp[i][j] += dp[i-1][j-1]
        if 0<=i-1:
            dp[i][j] += dp[i-1][j]
        if 0<=j-1:
            dp[i][j] += dp[i][j-1]
        # 重複カウント部分を減算する
        if 0<=i-1 and 0<=j-1:
            dp[i][j] -= dp[i-1][j-1]
        dp[i][j] %= mod j range(M+1) range M+1 M 1 if 0<=i-1 and 0<=j-1 and S[i-1]==T[j-1]:
            dp[i][j] += dp[i-1][j-1]
         0<=i-1 and 0<=j-1 and S[i-1]==T[j-1] 0<=i-1 and 0<=j-1 0<=i-1 0 i-1 i 1 0<=j-1 0 j-1 j 1 S[i-1]==T[j-1] S[i-1] S i-1 i 1 T[j-1] T j-1 j 1 dp[i][j] += dp[i-1][j-1] dp[i][j] [i] dp i j dp[i-1][j-1] [i-1] dp i-1 i 1 j-1 j 1 if 0<=i-1:
            dp[i][j] += dp[i-1][j]
         0<=i-1 0 i-1 i 1 dp[i][j] += dp[i-1][j] dp[i][j] [i] dp i j dp[i-1][j] [i-1] dp i-1 i 1 j if 0<=j-1:
            dp[i][j] += dp[i][j-1]
        # 重複カウント部分を減算する
         0<=j-1 0 j-1 j 1 dp[i][j] += dp[i][j-1] dp[i][j] [i] dp i j dp[i][j-1] [i] dp i j-1 j 1 if 0<=i-1 and 0<=j-1:
            dp[i][j] -= dp[i-1][j-1]
         0<=i-1 and 0<=j-1 0<=i-1 0 i-1 i 1 0<=j-1 0 j-1 j 1 dp[i][j] -= dp[i-1][j-1] dp[i][j] [i] dp i j dp[i-1][j-1] [i-1] dp i-1 i 1 j-1 j 1 dp[i][j] %= mod dp[i][j] [i] dp i j mod print(dp[N][M]) print dp[N][M] [N] dp N M M = map(int,input().split()) map(int,input().split()) M dp = [[0 for _ in range(M+1)] for _ in range(N+1)] [[0 for _ in range(M+1)] for _ in range(N+1)] dp input = sys.stdin.readline sys.stdin.readline input S = list(map(int,input().split())) list(map(int,input().split())) S mod = 10**9+7 10**9+7 mod N,M = map(int,input().split()) map(int,input().split()) N T = list(map(int,input().split())) list(map(int,input().split())) T
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

# dp[i][j] : Sの左からi文字目までと Tの左からj文字目まででの 共通部分文字列の数
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
dp[0][0] = 1

for i in range(N+1):
    for j in range(M+1):
        if 0<=i-1 and 0<=j-1 and S[i-1]==T[j-1]:
            dp[i][j] += dp[i-1][j-1]
        if 0<=i-1:
            dp[i][j] += dp[i-1][j]
        if 0<=j-1:
            dp[i][j] += dp[i][j-1]
        # 重複カウント部分を減算する
        if 0<=i-1 and 0<=j-1:
            dp[i][j] -= dp[i-1][j-1]
        dp[i][j] %= mod

print(dp[N][M])
