n = int(eval(input()))

xs = list(map(int, input().split()))

A = max(xs)

dp = [0] * (A+1)

MOD = 1000000007



r = 1

zc = 0

for x in xs:

    if x == 0:

        zc += 1

    else:

        r = r * dp[x-1] % MOD

        dp[x-1] -= 1

    dp[x] += 1

if zc == 0 or zc >= 4:

    rr = 0

elif zc == 1:

    rr = 3

else:

    rr = 6

print((r * rr % MOD))

#print(MOD)

#print(115295190)

eval(input())

dp,M,r=[3]+[0]*10**6,10**9+7,1

for x in map(int, input().split()):

    r=r*dp[x]%M

    dp[x]-=1

    dp[x+1]+=1

print(r)
n = int(eval(input()))
xs = list(map(int, input().split()))
A = max(xs)
dp = [0] * (A + 1)
MOD = 1000000007
r = 1
zc = 0
for x in xs:
    if x == 0:
        zc += 1
    else:
        r = r * dp[x - 1] % MOD
        dp[x - 1] -= 1
    dp[x] += 1
if zc == 0 or zc >= 4:
    rr = 0
elif zc == 1:
    rr = 3
else:
    rr = 6
print((r * rr % MOD))
# print(MOD)
# print(115295190)

eval(input())
dp, M, r = [3] + [0] * 10**6, 10**9 + 7, 1
for x in map(int, input().split()):
    r = r * dp[x] % M
    dp[x] -= 1
    dp[x + 1] += 1
print(r)

-n = int(eval(input())) -xs = list(map(int, input().split())) -A = max(xs) -dp = [0] * (A + 1) -MOD = 1000000007 -r = 1 -zc = 0 -for x in xs: -    if x == 0: -        zc += 1 -    else: -        r = r * dp[x - 1] % MOD -        dp[x - 1] -= 1 -    dp[x] += 1 -if zc == 0 or zc >= 4: -    rr = 0 -elif zc == 1: -    rr = 3 -else: -    rr = 6 -print((r * rr % MOD)) -# print(MOD) -# print(115295190) +eval(input()) +dp, M, r = [3] + [0] * 10**6, 10**9 + 7, 1 +for x in map(int, input().split()): +    r = r * dp[x] % M +    dp[x] -= 1 +    dp[x + 1] += 1 +print(r)