#include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(begin(x), end(x)), end(x))
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a==b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a==b)){ a=b; return true; } return false; }
template<typename T> inline bool isin(const T& x, const T& l, const T& r){ return l <= x and x < r; }
inline ostream& fcout(int n = 10){ return cout << fixed << setprecision(n); }
inline ostream& rcout(int n = 2){ return cout << right << setw(n); }
constexpr int64_t INF = numeric_limits<int64_t>::max();
constexpr int32_t INF32 = numeric_limits<int32_t>::max();
constexpr int64_t MOD = 1000000007LL;
constexpr double PI = 3.14159265358979323846264338327950288;
constexpr double EPS = 1e-10;


template <uint_fast64_t Modulus>
struct ModInt {
    using u64 = uint_fast64_t;
    u64 val;
    constexpr ModInt(const int x = 0) noexcept : val(x >= 0 ? x%Modulus : x%(int)Modulus+Modulus){}
    constexpr const u64& value() const noexcept { return val; }
    constexpr ModInt operator-() const noexcept { return val ? Modulus-val : 0; }
    constexpr ModInt operator+(const ModInt& rhs) const noexcept { return ModInt(*this) += rhs; }
    constexpr ModInt operator-(const ModInt& rhs) const noexcept { return ModInt(*this) -= rhs; }
    constexpr ModInt operator*(const ModInt& rhs) const noexcept { return ModInt(*this) *= rhs; }
    constexpr ModInt operator/(const ModInt& rhs) const noexcept { return ModInt(*this) /= rhs; }
    constexpr bool operator==(const ModInt& rhs) const noexcept { return val == rhs.val; }
    constexpr bool operator!=(const ModInt& rhs) const noexcept { return val != rhs.val; }
    constexpr ModInt& operator+=(const ModInt& rhs) noexcept {
        val += rhs.val;
        if(val >= Modulus) val -= Modulus;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& rhs) noexcept {
        if(val < rhs.val) val += Modulus;
        val -= rhs.val;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& rhs) noexcept {
        val = val*rhs.val%Modulus;
        return *this;
    }
    constexpr ModInt& operator/=(const ModInt& rhs) noexcept {
        *this *= rhs.inverse();
        return *this;
    }
    constexpr ModInt inverse() const noexcept {
        int a = val, b = Modulus, u = 1, v = 0;
        while(b){
            int t = a/b;
            a -= t*b; swap(a, b);
            u -= t*v; swap(u, v);
        }
        return ModInt(u);
    }
    constexpr ModInt pow(u64 n) const noexcept {
        ModInt res(1), mul(val);
        while(n > 0){
            if(n&1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    friend constexpr ModInt operator+(const int lhs, const ModInt& rhs) noexcept { return rhs+lhs; }
    friend constexpr ModInt operator*(const int lhs, const ModInt& rhs) noexcept { return rhs*lhs; }
    friend ostream& operator<<(ostream& os, const ModInt& rhs){ return os << rhs.val; }
    friend istream& operator>>(istream& is, ModInt& rhs){
        u64 t;
        is >> t;
        rhs = ModInt<Modulus>(t);
        return is;
    }
};
using modint = ModInt<MOD>;


int N;
int A[100010];
VI b[100010];


int32_t main(){
    cin >> N;
    rep(i, 0, N) cin >> A[i];

    rep(i, 0, N){
        b[A[i]].push_back(i);
        if(SZ(b[A[i]]) > 3){ cout << 0 << endl; return 0; }
    }

    modint ans = 1;
    rep(i, 0, N){
        int c;
        if(A[i] == 0) c = 3;
        else c = count_if(ALL(b[A[i]-1]), [&](int x){ return x < i; });
        c -= count_if(ALL(b[A[i]]), [&](int x){ return x < i; });
        ans *= c;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 9808