import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))

mod = 10 ** 9 + 7
note = []
total = 1

for i in range(N):
	count = 1
	for j in range(M):
		if S[i] == T[j]:
			note.append((S[i], j))
			count += 1
	total = (total * count) % mod
	
print(total)