#include <bits/stdc++.h>

int main() {
    using namespace std;
    unsigned long N;
    cin >> N;
    unsigned long a{0}, b{0}, c{0};
    unsigned long ans{1};
    for(unsigned long _ = 0, i; _ < N; ++_){
        cin >> i;
        auto k{0UL};
        k += (a == i) + (b == i) + (c == i);
        (ans *= k) %= 1000000007;
        if(a == i)++a;
        else if(b == i)++b;
        else if(c == i)++c;
    }
    cout << ans << endl;
    return 0;
}