import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10**9 + 7

N,M = map(int,readline().split())
S = list(map(int,readline().split()))
T = np.array(['0']+readline().split(),np.int32)

# Sは左から見ていく。
# Tについて、（最後に一致させた文字の位置、通り数）をdpで持つ

dp = np.zeros(M+1,dtype=np.int64)
dp[0] = 1

for i,s in enumerate(S,1):
    prev = dp
    prev_cum = prev.cumsum() % MOD
    equal = (s == T)
    dp[1:] += equal[1:] * prev_cum[:-1]
    dp[dp>=MOD] -= MOD

answer = dp.sum() % MOD
print(answer) import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines import numpy as np numpy MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 N,M = map(int,readline().split()) N map(int,readline().split()) map int readline().split() ().split () readline split M S = list(map(int,readline().split())) S list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split T = np.array(['0']+readline().split(),np.int32) T np.array(['0']+readline().split(),np.int32) np.array np array ['0']+readline().split() ['0'] '0' readline().split() ().split () readline split np.int32 np int32 dp = np.zeros(M+1,dtype=np.int64) dp np.zeros(M+1,dtype=np.int64) np.zeros np zeros M+1 M 1 dtype=np.int64 dtype np.int64 np int64 dp[0] = 1 dp[0] dp 0 1 for i,s in enumerate(S,1):
    prev = dp
    prev_cum = prev.cumsum() % MOD
    equal = (s == T)
    dp[1:] += equal[1:] * prev_cum[:-1]
    dp[dp>=MOD] -= MOD i s enumerate(S,1) enumerate S 1 prev = dp prev dp prev_cum = prev.cumsum() % MOD prev_cum prev.cumsum() % MOD prev.cumsum() prev.cumsum prev cumsum MOD equal = (s == T) equal s == T s T dp[1:] += equal[1:] * prev_cum[:-1] dp[1:] dp 1: 1 equal[1:] * prev_cum[:-1] equal[1:] equal 1: 1 prev_cum[:-1] prev_cum :-1 -1 dp[dp>=MOD] -= MOD dp[dp>=MOD] dp dp>=MOD dp MOD MOD answer = dp.sum() % MOD answer dp.sum() % MOD dp.sum() dp.sum dp sum MOD print(answer) print answer dp = np.zeros(M+1,dtype=np.int64) np.zeros(M+1,dtype=np.int64) dp S = list(map(int,readline().split())) list(map(int,readline().split())) S readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline N,M = map(int,readline().split()) map(int,readline().split()) N prev_cum = prev.cumsum() % MOD prev.cumsum() % MOD prev_cum equal = (s == T) s == T equal readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines prev = dp dp prev M = map(int,readline().split()) map(int,readline().split()) M answer = dp.sum() % MOD dp.sum() % MOD answer MOD = 10**9 + 7 10**9 + 7 MOD read = sys.stdin.buffer.read sys.stdin.buffer.read read T = np.array(['0']+readline().split(),np.int32) np.array(['0']+readline().split(),np.int32) T
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10**9 + 7

N,M = map(int,readline().split())
S = list(map(int,readline().split()))
T = np.array(['0']+readline().split(),np.int32)

# Sは左から見ていく。
# Tについて、（最後に一致させた文字の位置、通り数）をdpで持つ

dp = np.zeros(M+1,dtype=np.int64)
dp[0] = 1

for i,s in enumerate(S,1):
    prev = dp
    prev_cum = prev.cumsum() % MOD
    equal = (s == T)
    dp[1:] += equal[1:] * prev_cum[:-1]
    dp[dp>=MOD] -= MOD

answer = dp.sum() % MOD
print(answer)