N,M = map(int,input().split())
if N < M:
    N,M = M,N
    T = [int(x) for x in input().split()]
    S = [int(x) for x in input().split()]
else:
    S = [int(x) for x in input().split()]
    T = [int(x) for x in input().split()]

ans = 0
indl = ['#' for i in range(100001)]
sums = [0 for i in range(M)]

for i in range(N):
    a = S[i]
    if indl[a] == '#':
        indexes = [j for j, x in enumerate(T) if x == a]
        indl[a] = indexes
    else:   indexes = indl[a]
    
    for ind in indexes[::-1]:
        temp = 1 + sums[ind]
        ans += temp
        sums = sums[:ind+1] + [x + temp for x in sums[ind+1:]]
        
print((ans+1)%(1000000007))