N = int(eval(input()))

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



c = [-1, -1 ,-1]



sum = 1

tmp = 0

for i in range(N):

    sum = sum * c.count(A[i]-1) % 1000000007

    if (A[i]-1) == c[0]:

        c[0] += 1

        tmp = c.count(c[0] - 1) + 1

    elif (A[i]-1) == c[1]:

        c[1] += 1

        tmp = c.count(c[1]-1) + 1

    elif (A[i]-1) == c[2]:

        c[2] += 1

        tmp = c.count(c[2] - 1) + 1

print(sum)

N = int(eval(input()))

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



c = [-1, -1 ,-1]



sum = 1

tmp = 0

for a in A:

    sum = sum * c.count(a-1) % 1000000007

    if a-1 == c[0]:

        c[0] += 1

        tmp = c.count(c[0] - 1) + 1

    elif a-1 == c[1]:

        c[1] += 1

        tmp = c.count(c[1]-1) + 1

    elif a-1 == c[2]:

        c[2] += 1

        tmp = c.count(c[2] - 1) + 1

print(sum)

N = int(eval(input()))
A = list(map(int, input().split()))
c = [-1, -1, -1]
sum = 1
tmp = 0
for i in range(N):
    sum = sum * c.count(A[i] - 1) % 1000000007
    if (A[i] - 1) == c[0]:
        c[0] += 1
        tmp = c.count(c[0] - 1) + 1
    elif (A[i] - 1) == c[1]:
        c[1] += 1
        tmp = c.count(c[1] - 1) + 1
    elif (A[i] - 1) == c[2]:
        c[2] += 1
        tmp = c.count(c[2] - 1) + 1
print(sum)

N = int(eval(input()))
A = list(map(int, input().split()))
c = [-1, -1, -1]
sum = 1
tmp = 0
for a in A:
    sum = sum * c.count(a - 1) % 1000000007
    if a - 1 == c[0]:
        c[0] += 1
        tmp = c.count(c[0] - 1) + 1
    elif a - 1 == c[1]:
        c[1] += 1
        tmp = c.count(c[1] - 1) + 1
    elif a - 1 == c[2]:
        c[2] += 1
        tmp = c.count(c[2] - 1) + 1
print(sum)

-for i in range(N): -    sum = sum * c.count(A[i] - 1) % 1000000007 -    if (A[i] - 1) == c[0]: +for a in A: +    sum = sum * c.count(a - 1) % 1000000007 +    if a - 1 == c[0]: -    elif (A[i] - 1) == c[1]: +    elif a - 1 == c[1]: -    elif (A[i] - 1) == c[2]: +    elif a - 1 == c[2]: