N = int(eval(input()))

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

MOD = 10**9+7



ans = 1

dp = [0]*3

for a in A:

    cnt = 0

    for i in range(3):

        if a == dp[i]:

            cnt += 1

    for i in range(3):

        if a == dp[i]:

            dp[i] += 1

            break

    ans = ans*cnt % MOD

print(ans)

N = int(eval(input()))

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

MOD = 10**9+7



ans = 1

dp = [0]*3

for a in A:

    ans = ans*dp.count(a) % MOD

    try:

        dp[dp.index(a)] += 1

    except ValueError:

        break

print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
MOD = 10**9 + 7
ans = 1
dp = [0] * 3
for a in A:
    cnt = 0
    for i in range(3):
        if a == dp[i]:
            cnt += 1
    for i in range(3):
        if a == dp[i]:
            dp[i] += 1
            break
    ans = ans * cnt % MOD
print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
MOD = 10**9 + 7
ans = 1
dp = [0] * 3
for a in A:
    ans = ans * dp.count(a) % MOD
    try:
        dp[dp.index(a)] += 1
    except ValueError:
        break
print(ans)

-    cnt = 0 -    for i in range(3): -        if a == dp[i]: -            cnt += 1 -    for i in range(3): -        if a == dp[i]: -            dp[i] += 1 -            break -    ans = ans * cnt % MOD +    ans = ans * dp.count(a) % MOD +    try: +        dp[dp.index(a)] += 1 +    except ValueError: +        break