n,m=map(int,input().split())
s=list(map(int,input().split()))
t=list(map(int,input().split()))

waru=10**9+7

ans=1

dic_s={}
dic_t={}

for i in range(2**n):
    li=0
    for j in range(n):
        if (i>>j) & 1:
            li*=10
            li+=s[j]
    if li in dic_s.keys():
        dic_s[li]+=1
    else:
        dic_s[li]=1

for i in range(2**m):
    li=0
    for j in range(m):
        if (i>>j) & 1:
            li*=10
            li+=t[j]
    if li in dic_t.keys():
        dic_t[li]+=1
    else:
        dic_t[li]=1

count=0
for i in dic_s.keys():
    if i in dic_t.keys():
        count+=dic_s[i]*dic_t[i]

print(count%waru)