# まずDPを使う理由はあるs[i]=t[j]の時 そのs[i],t[j]を追加した時に増える部分列の場合の数は前の部分列の場合の数+1増えるから
# 前の要素を利用するということはDPを疑う
import sys
input=sys.stdin.readline

mod = 10**9 + 7
n,m = map(int, input().split())
s = input().split()
t = input().split()

dp = [[0] * (m+1) for i in range(n+1)]
S = [[0] * (m+1) for i in range(n+1)]

for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] += (S[i][j] + 1)
            dp[i+1][j+1] %= mod
        else:
            dp[i+1][j+1] = 0 # i番目,j番目を追加すると増える場合の数 iとjが等しくないならば場合の数は増えないから0
        S[i+1][j+1] = S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j]
        S[i+1][j+1] %= mod

print(S[n][m]+1) import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 n,m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m s = input().split() s input().split() ().split () input split t = input().split() t input().split() ().split () input split [0] * (m+1) for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [0] * (m+1) [0] 0 m+1 m 1 dp = [[0] * (m+1) for i in range(n+1)] dp [[0] * (m+1) for i in range(n+1)] [0] * (m+1) for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [0] * (m+1) [0] 0 m+1 m 1 S = [[0] * (m+1) for i in range(n+1)] S [[0] * (m+1) for i in range(n+1)] for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] += (S[i][j] + 1)
            dp[i+1][j+1] %= mod
        else:
            dp[i+1][j+1] = 0 # i番目,j番目を追加すると増える場合の数 iとjが等しくないならば場合の数は増えないから0
        S[i+1][j+1] = S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j]
        S[i+1][j+1] %= mod i range(n) range n for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] += (S[i][j] + 1)
            dp[i+1][j+1] %= mod
        else:
            dp[i+1][j+1] = 0 # i番目,j番目を追加すると増える場合の数 iとjが等しくないならば場合の数は増えないから0
        S[i+1][j+1] = S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j]
        S[i+1][j+1] %= mod j range(m) range m if s[i] == t[j]:
            dp[i+1][j+1] += (S[i][j] + 1)
            dp[i+1][j+1] %= mod
        else:
            dp[i+1][j+1] = 0 # i番目,j番目を追加すると増える場合の数 iとjが等しくないならば場合の数は増えないから0
         s[i] == t[j] s[i] s i t[j] t j dp[i+1][j+1] += (S[i][j] + 1) dp[i+1][j+1] [i+1] dp i+1 i 1 j+1 j 1 S[i][j] + 1 S[i][j] [i] S 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 dp[i+1][j+1] = 0 dp[i+1][j+1] [i+1] dp i+1 i 1 j+1 j 1 0 S[i+1][j+1] = S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j] S[i+1][j+1] [i+1] S i+1 i 1 j+1 j 1 S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j] S[i][j+1] + S[i+1][j] + dp[i+1][j+1] S[i][j+1] + S[i+1][j] S[i][j+1] [i] S i j+1 j 1 S[i+1][j] [i+1] S i+1 i 1 j dp[i+1][j+1] [i+1] dp i+1 i 1 j+1 j 1 S[i][j] [i] S i j S[i+1][j+1] %= mod S[i+1][j+1] [i+1] S i+1 i 1 j+1 j 1 mod print(S[n][m]+1) print S[n][m]+1 S[n][m] [n] S n m 1 dp = [[0] * (m+1) for i in range(n+1)] [[0] * (m+1) for i in range(n+1)] dp mod = 10**9 + 7 10**9 + 7 mod s = input().split() input().split() s n,m = map(int, input().split()) map(int, input().split()) n input=sys.stdin.readline sys.stdin.readline input t = input().split() input().split() t m = map(int, input().split()) map(int, input().split()) m S = [[0] * (m+1) for i in range(n+1)] [[0] * (m+1) for i in range(n+1)] S
# まずDPを使う理由はあるs[i]=t[j]の時 そのs[i],t[j]を追加した時に増える部分列の場合の数は前の部分列の場合の数+1増えるから
# 前の要素を利用するということはDPを疑う
import sys
input=sys.stdin.readline

mod = 10**9 + 7
n,m = map(int, input().split())
s = input().split()
t = input().split()

dp = [[0] * (m+1) for i in range(n+1)]
S = [[0] * (m+1) for i in range(n+1)]

for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] += (S[i][j] + 1)
            dp[i+1][j+1] %= mod
        else:
            dp[i+1][j+1] = 0 # i番目,j番目を追加すると増える場合の数 iとjが等しくないならば場合の数は増えないから0
        S[i+1][j+1] = S[i][j+1] + S[i+1][j] + dp[i+1][j+1] - S[i][j]
        S[i+1][j+1] %= mod

print(S[n][m]+1)