#include <bits/stdc++.h>
using namespace std;

#define int long long
#define MOD 1000000007

int n;
map<int, int> mp;
int a[100010];
int pt[100010];

signed main(){
    cin >> n;

    for(int i = 0;i < n;i++){
        cin >> a[i];
    }

    mp[-1] = 3;
    int ans = 1;
    for(int i = 0;i < n;i++){
        pt[i] = mp[a[i]-1] - mp[a[i]];
        mp[a[i]]++;
        ans *= pt[i];
        ans %= MOD;
    }

    cout << ans << endl;
    return 0;
}
