#include<bits/extc++.h>
#ifndef __unix__
#define fread_unlocked fread
#endif
#ifdef _debug 
#define dout(i) cout << #i << ' ' << i << ' '
#else 
#include<bits/stdc++.h>
#define dout(i) 0&&0
#endif
#pragma GCC optimize("unroll-loops,no-stack-protector,O3")
#define priority_queue __gnu_pbds::priority_queue
#define unordered_map __gnu_pbds::gp_hash_table
#define pb emplace_back
#define rep(_n) for(int __i = _n;__i--;)
#define rep0(_i, _n) for(int _i = 0; (_i)<(_n) ;++_i)
#define rep1(_i, _n) for(int _i = 1; (_i)<=(_n) ;++_i)
#define yn(i) ((i)?"Yes" : "No")
#define eout(i) {cout << (i) << '\n';exit(0);}
#define rout(i) {return cout << (i) << '\n', 0;}
#define multi int _T;for(read(_T);_T--;)
using namespace std;
using ll = long long;using ull = unsigned long long;using ul = unsigned;using db = double;
using __gnu_pbds::null_type;
inline char readchar(){static const unsigned bufsize = 1 << 16; static unsigned p = 0, end = 0; static char buf[bufsize]; if (p == end) end = fread_unlocked(buf, sizeof(char), bufsize, stdin), p = 0; return p == end ? EOF : buf[p++]; }
inline void read(char &a){while(isspace(a = readchar()));}
inline void read(char *a){while(isspace(*a = readchar()));++a; while(isgraph(*a = readchar()))++a;}
inline void read(string &a){static char p; while(isspace(p = readchar())); a = p; while(isgraph(p = readchar()))a.push_back(p);}
template<typename T> inline void read(T& a){static char p; static bool b;while (!isdigit(p = readchar()))b = p == '-'; a = p ^ '0'; while (isdigit(p = readchar())) a *= 10, a += p ^ '0'; if(b)a *= -1, b = false; }
template<typename T1, typename T2> inline void read(pair<T1, T2> &a){read(a.first), read(a.second);}
template<typename T> inline void read(T begin, const T &end){while(begin != end)read(*begin), ++begin;}
inline void R(){} template<class T1, class... T2> inline void R(T1 &h, T2 &... e){read(h), R(e...);}
template<typename T> inline void write(T begin, const T &end){while(begin != end)cout << *begin << " \n"[next(begin) == end], ++begin;}
template<typename T> using rbtree = __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T> inline ostream& operator << (ostream &ou, const vector<T> &a){for(auto it = a.begin();it != a.end();++it)ou << (it != a.begin() ? " " : "") << *it;return ou;}
template<typename T1, typename T2>inline istream& operator >> (istream &in, pair<T1, T2> &a){return in >> a.first >> a.second;}
template<typename T1, typename T2>inline ostream& operator << (ostream &ou, const pair<T1, T2> &b){return ou << b.first << ' ' << b.second;}
template<typename T> inline long long pow(long long v, long long t, const T &p){long long res = 1;for(;t;t >>= 1, v = v * v % p)if(t & 1)res = res * v % p;return res;}
const int maxn = 300010;
const int p = 1000000007;
int a[maxn];
ll res = 1, cnt[maxn], tc[maxn];
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n;
	R(n);
	read(a, a+n);
	int z = 0;
	rep0(i, n){
		if(a[i]){
			if(cnt[a[i]-1] == 0)rout(0);
			res = res * cnt[a[i]-1] % p;
			--cnt[a[i]-1];
		}
		else ++z;
		++cnt[a[i]];
		if(++tc[a[i]] > 3)rout(0);
	}
	cout << res*(z == 1 ? 3 : 6) % p << '\n';
}


Measured memory_bytes: 3584