N, M = map(int, input().split())
*S, = map(int, input().split())
*T, = map(int, input().split())

MOD = 10**9 + 7
P = []
for i in range(N):
    for j in range(M):
        if S[i] == T[j]:
            P.append((i, j))
P.sort(key=lambda x: (x[0], -x[1]))

data = [0]*(M+2)
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s % MOD
def add(k, x):
    while k <= M:
        data[k] = (data[k] + x) % MOD
        k += k & -k

s = 0
for p, q in P:
    v = get(q+1) + 1
    add(q+2, v); s += v
print((s + 1) % MOD)