# coding: utf-8
# Your code here!
import collections

N,M=map(int,input().split())

S=list(map(int,input().split()))
T=list(map(int,input().split()))

ans_s=[0]
for i in range(N):
    temp=[]
    for j in range(len(ans_s)):
        ans_s.append(ans_s[j]*10+S[i])
#print(ans_s)


ans_t=[0]
for i in range(M):
    temp=[]
    for j in range(len(ans_t)):
        #print(ans_t[j],T[i])
        ans_t.append(ans_t[j]*10+T[i])
#print(ans_t)

cand_s=collections.Counter(ans_s)
cand_t=collections.Counter(ans_t)

ans=0
for item in cand_s:
    ans+=cand_s[item]*cand_t[item]

print(ans%(10**9+7))