#include <bits/stdc++.h>

#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define px pair<db, db>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define input(a) for (auto& el : a) cin >> el;
#define output(a) cout << '!'; for (auto& el : a) cout << el << ' '; cout << '\n'; cout.flush();
#define sz(el) int(el.size())
#define sq(a) ((a) * (a))
#define xll bitset<128>
#define xmin(a,b) a = min(a, b)
#define xmax(a,b) a = max(a, b)
#define sun(a) sort(all(a)); a.resize(unique(all(a)) - a.begin());
/**/  /// extra
//#define forn(i,n) for (int i = 0; i < n; ++i)
//#define rforn(i,n) for (int i = n - 1; i >= 0; --i)
#define bn '\n'
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vll vector<vl>
//#define G vector<vector<pii>>
#define GVE vector<vector<edge>>
#define usi unordered_set<int>
#define usp set<pii>
/**/

using namespace std;

const int INF = 1'000'000'017;
const ll LLINF = 1'000'000'000'000'000'017LL;
const ld inf = 1.0 / 0.0;
const ld eps = 1e-9;
const int MOD = 1000000007;

//mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

//uniform_int_distribution<int> uid(l, r); [l, r]
//int x = uid(rng);

/// Author: @mathalex
namespace modint {
    static const int MODx = 1e9 + 7;

    struct mint {
        int a = 0;

        mint() {}

        mint(int a) : a(a) {}

        mint operator+(const mint& b) const {
            int res = a + b.a;
            if (res >= MODx) {
                res -= MODx;
            }
            return res;
        }

        void operator+=(const mint& b) {
            a += b.a;
            if (a >= MODx) {
                a -= MODx;
            }
        }

        mint operator-(const mint& b) const {
            int res = a - b.a;
            if (res < 0) {
                res += MODx;
            }
            return res;
        }

        void operator-=(const mint& b) {
            a -= b.a;
            if (a < 0) {
                a += MODx;
            }
        }

        mint operator*(const mint& b) const {
            return (ll) a * b.a % MODx;
        }

        void operator*=(const mint& b) {
            a = (ll) a * b.a % MODx;
        }

        int get() const {
            return a;
        }
    };

    mint fpow(mint a, int p) {
        mint x = 1;
        while (p) {
            if (p & 1) {
                x *= a;
            }
            a *= a;
            p >>= 1;
        }
        return x;
    }

    istream& operator>>(istream& in, mint& a) {
        return (in >> a.a);
    }

    ostream& operator<<(ostream& out, const mint& a) {
        return (out << a.a);
    }
}

using namespace modint;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<mint> cnt(n + 1);
    cnt[0].a = 3;
    mint ans = 1;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        ++x;
        ans *= cnt[x - 1];
        cnt[x - 1] -= 1;
        cnt[x] += 1;
    }
    cout << ans;
    return 0;
}


Measured memory_bytes: 640