import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

n,m = inm()
s = inl()
t = inl()
dp = [ [0] * (m+1) for i in range(n) ]
dp[0][0] = 1 if s[0]==t[0] else 0
for i in range(1,n):
    dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0)
for j in range(1,m):
    dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0)

sh = defaultdict(list)
for i in range(1,n):
    sh[s[i]].append(i)

for j in range(1,m):
    x = 0
    acc = [0] * (n+1)
    for z in sh[t[j]]:  # s[z]==t[j]
        x = (x+dp[z-1][j-1]+1)%R  # z>0
        acc[z] = x
    for i in range(1,n):
        acc[i] = (acc[i-1] if acc[i]==0 else acc[i])

    for i in range(1,n):
        x = dp[i][j-1]
        #for z in sh[t[j]]:  # s[z]==t[j]
        #  if z<=i:
        #    x = (x+dp[z-1][j-1]+1)%R  # z>0
        x += acc[i]
        if t[j]==s[0]:
            x += 1
        dp[i][j] = x%R

if DBG:
    for i in range(n):
        print(dp[i])
print((dp[n-1][m-1]+1)%R) import sys,heapq sys heapq from collections import deque,defaultdict printn = lambda x: sys.stdout.write(x) printn lambda x: sys.stdout.write(x) sys.stdout.write(x) sys.stdout.write sys.stdout sys stdout write x x inn = lambda : int(input()) inn lambda : int(input()) int(input()) int input() input inl   = lambda: list(map(int, input().split())) inl lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split inm   = lambda:      map(int, input().split()) inm lambda:      map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split DBG = True  and False DBG True  and False True False R = 10**9 + 7 R 10**9 + 7 10**9 10 9 7 def ddprint(x):
  if DBG:
    print(x) ddprint if DBG:
    print(x) DBG print(x) print x x x n,m = inm() n inm() inm m s = inl() s inl() inl t = inl() t inl() inl [0] * (m+1) for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0] * (m+1) [0] 0 m+1 m 1 dp = [ [0] * (m+1) for i in range(n) ] dp [ [0] * (m+1) for i in range(n) ] dp[0][0] = 1 if s[0]==t[0] else 0 dp[0][0] [0] dp 0 0 1 if s[0]==t[0] else 0 s[0]==t[0] s[0] s 0 t[0] t 0 1 0 for i in range(1,n):
    dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0) i range(1,n) range 1 n dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0) dp[i][0] [i] dp i 0 dp[i-1][0] + (1 if s[i]==t[0] else 0) dp[i-1][0] [i-1] dp i-1 i 1 0 1 if s[i]==t[0] else 0 s[i]==t[0] s[i] s i t[0] t 0 1 0 for j in range(1,m):
    dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0) j range(1,m) range 1 m dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0) dp[0][j] [0] dp 0 j dp[0][j-1] + (1 if s[0]==t[j] else 0) dp[0][j-1] [0] dp 0 j-1 j 1 1 if s[0]==t[j] else 0 s[0]==t[j] s[0] s 0 t[j] t j 1 0 sh = defaultdict(list) sh defaultdict(list) defaultdict list for i in range(1,n):
    sh[s[i]].append(i) i range(1,n) range 1 n sh[s[i]].append(i) [s[i]].append [s[i]] sh s[i] s i append i for j in range(1,m):
    x = 0
    acc = [0] * (n+1)
    for z in sh[t[j]]:  # s[z]==t[j]
        x = (x+dp[z-1][j-1]+1)%R  # z>0
        acc[z] = x
    for i in range(1,n):
        acc[i] = (acc[i-1] if acc[i]==0 else acc[i])

    for i in range(1,n):
        x = dp[i][j-1]
        #for z in sh[t[j]]:  # s[z]==t[j]
        #  if z<=i:
        #    x = (x+dp[z-1][j-1]+1)%R  # z>0
        x += acc[i]
        if t[j]==s[0]:
            x += 1
        dp[i][j] = x%R j range(1,m) range 1 m x = 0 x 0 acc = [0] * (n+1) acc [0] * (n+1) [0] 0 n+1 n 1 for z in sh[t[j]]:  # s[z]==t[j]
        x = (x+dp[z-1][j-1]+1)%R  # z>0
        acc[z] = x
     z sh[t[j]] sh t[j] t j x = (x+dp[z-1][j-1]+1)%R x (x+dp[z-1][j-1]+1)%R x+dp[z-1][j-1]+1 x+dp[z-1][j-1] x dp[z-1][j-1] [z-1] dp z-1 z 1 j-1 j 1 1 R acc[z] = x acc[z] acc z x for i in range(1,n):
        acc[i] = (acc[i-1] if acc[i]==0 else acc[i])

     i range(1,n) range 1 n acc[i] = (acc[i-1] if acc[i]==0 else acc[i]) acc[i] acc i acc[i-1] if acc[i]==0 else acc[i] acc[i]==0 acc[i] acc i 0 acc[i-1] acc i-1 i 1 acc[i] acc i for i in range(1,n):
        x = dp[i][j-1]
        #for z in sh[t[j]]:  # s[z]==t[j]
        #  if z<=i:
        #    x = (x+dp[z-1][j-1]+1)%R  # z>0
        x += acc[i]
        if t[j]==s[0]:
            x += 1
        dp[i][j] = x%R i range(1,n) range 1 n x = dp[i][j-1] x dp[i][j-1] [i] dp i j-1 j 1 x += acc[i] x acc[i] acc i if t[j]==s[0]:
            x += 1
         t[j]==s[0] t[j] t j s[0] s 0 x += 1 x 1 dp[i][j] = x%R dp[i][j] [i] dp i j x%R x R if DBG:
    for i in range(n):
        print(dp[i]) DBG for i in range(n):
        print(dp[i]) i range(n) range n print(dp[i]) print dp[i] dp i print((dp[n-1][m-1]+1)%R) print (dp[n-1][m-1]+1)%R dp[n-1][m-1]+1 dp[n-1][m-1] [n-1] dp n-1 n 1 m-1 m 1 1 R inm   = lambda:      map(int, input().split()) lambda:      map(int, input().split()) inm m = inm() inm() m x = dp[i][j-1] dp[i][j-1] x x = 0 0 x printn = lambda x: sys.stdout.write(x) lambda x: sys.stdout.write(x) printn dp = [ [0] * (m+1) for i in range(n) ] [ [0] * (m+1) for i in range(n) ] dp acc = [0] * (n+1) [0] * (n+1) acc s = inl() inl() s sh = defaultdict(list) defaultdict(list) sh n,m = inm() inm() n inn = lambda : int(input()) lambda : int(input()) inn x += acc[i] acc[i] x x += 1 1 x t = inl() inl() t x = (x+dp[z-1][j-1]+1)%R (x+dp[z-1][j-1]+1)%R x R = 10**9 + 7 10**9 + 7 R def ddprint(x):
  if DBG:
    print(x) def ddprint(x):
  if DBG:
    print(x) ddprint DBG = True  and False True  and False DBG inl   = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) inl
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

n,m = inm()
s = inl()
t = inl()
dp = [ [0] * (m+1) for i in range(n) ]
dp[0][0] = 1 if s[0]==t[0] else 0
for i in range(1,n):
    dp[i][0] = dp[i-1][0] + (1 if s[i]==t[0] else 0)
for j in range(1,m):
    dp[0][j] = dp[0][j-1] + (1 if s[0]==t[j] else 0)

sh = defaultdict(list)
for i in range(1,n):
    sh[s[i]].append(i)

for j in range(1,m):
    x = 0
    acc = [0] * (n+1)
    for z in sh[t[j]]:  # s[z]==t[j]
        x = (x+dp[z-1][j-1]+1)%R  # z>0
        acc[z] = x
    for i in range(1,n):
        acc[i] = (acc[i-1] if acc[i]==0 else acc[i])

    for i in range(1,n):
        x = dp[i][j-1]
        #for z in sh[t[j]]:  # s[z]==t[j]
        #  if z<=i:
        #    x = (x+dp[z-1][j-1]+1)%R  # z>0
        x += acc[i]
        if t[j]==s[0]:
            x += 1
        dp[i][j] = x%R

if DBG:
    for i in range(n):
        print(dp[i])
print((dp[n-1][m-1]+1)%R)
