#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

template<typename T>
static inline void chmin(T& ref, const T  value) {
    if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T& ref, const T value) {
    if (ref < value) ref = value;
}

constexpr ll mod = 1000000007;

class modint {
	static constexpr std::int_fast64_t Modulus = 1e9 + 7;
	using u64 = std::int_fast64_t;

public:
	u64 a;
	constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
	constexpr u64& value() noexcept { return a; }
	constexpr const u64& value() const noexcept { return a; }
	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) noexcept {
		a += rhs.a;
		if (a >= Modulus) {
			a -= Modulus;
		}
		return *this;
	}
	constexpr modint& operator-=(const modint rhs) noexcept {
		if (a < rhs.a) {
			a += Modulus;
		}
		a -= rhs.a;
		return *this;
	}
	constexpr modint& operator*=(const modint rhs) noexcept {
		a = a * rhs.a % Modulus;
		return *this;
	}
};

int main() {
    int n;
    cin >> n;
    vector<int> vs(n);
    rep(i, n) {
        cin >> vs[i];
    }
    vector<int> colors(3, 0);
    modint all = 1;
    rep(i, n) {
		int same = 0;
		rep(j, 3) {
			if (colors[j] == vs[i]) same++;
		}
		all *= same;
		rep(j, 3) {
			if (colors[j] == vs[i]) {
				colors[j]++;
				break;
			}
		}
    }
	cout << all.a << endl;
    return 0;
}


#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    ll MOD = pow(10, 9) + 7;
    vector<ll> A(N);
    rep(i, N) {
        cin >> A[i];
        A[i]++;
    }

    vector<ll> dp(N, 0);
    ll ans = 1;
    dp[0] = 3;

    for (int i = 0; i < N; i++) {
        if (dp[A[i] - 1] == 0) {
            cout << 0 << endl;
            return 0;
        }

        ans *= dp[A[i] - 1];
        ans %= MOD;
        dp[A[i] - 1]--;
        dp[A[i]]++;
    }

    cout << ans << endl;
}
