import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;

    auto cnt0 = new long[](N + 1);
    auto cnt1 = new long[](N + 1);
    long ans = 1;

    foreach (a; A) {
        if (a == 0) {
            cnt0[0] += 1;
        } else if (cnt[a - 1] > 0) {
            ans = ans * (cnt0[a - 1]) % MOD;
            cnt0[a - 1] -= 1;
            cnt0[a] += 1;
        } else {
            writeln(0);
            return;
        }
        cnt1[a] += 1;
    }

    long hoge = A.map!(a => a == 0).sum;
    hoge = hoge == 1 ? 3 : 6;

    if (cnt1.map!(a => a > 3).any) {
        writeln(0);
        return;dmd
    }

    ans = ans * hoge % MOD;
    ans.writeln;
}