#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int main() {
    int n; cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<long long> count(n+1, 0);
    count[0] = 3;
    long long ans = 1;
    for (int i = 0; i < n; i++) {
        ans = ans * count[a[i]] % mod;
        count[a[i]]--; count[a[i]+1]++;
    }
    cout << ans << endl;
    return 0;
}