import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

enum P = 10L^^9+7;

void main()
{
    auto N = readln.chomp.to!int;
    int x, y, z;
    long r = 1;
    foreach (a; readln.split.to!(int[])) {
        int d;
        if (a == x) ++d;
        if (a == y) ++d;
        if (a == z) ++d;
        if (d == 3) {
            r = (r * 3) % P;
        } else if (d == 2) {
            r = (r * 2) % P;
        }
        if (a == x) {
            ++x;
        } else if (a == y) {
            ++y;
        } else {
            ++z;
        }
    }
    writeln(r);
}