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 for i in range(M)]
indl = ['#' for i in range(100001)]

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]:
        ans[ind] += 1 + sum(ans[:ind])

print((sum(ans)+1)%(1000000007))