stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

#include <numeric>

#include <functional>

#include <queue>

#include <stack>

#include <bitset>

#include <set>

#include <chrono>

#include <string>

#include <assert.h>



using namespace std;



#define endl '\n'



int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    int a, b, c;

    cin >> a >> b >> c;



    if (a == b && a != c)

        cout << "Yes" << endl;

    else if (a == c && a != b)

        cout << "Yes" << endl;

    else if (c == b && a != c)

        cout << "Yes" << endl;

    else

        cout << "No" << endl;



    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



 int main(void){

 	int a, b, c;

 	scanf("%d%d%d", &a, &b, &c);



    int ispoor = 0;

    if (a == b && b != c) ispoor = 1;

    if (b == c && c != a) ispoor = 1;

    if (c == a && a != b) ispoor = 1;



    if (ispoor) {

        puts("Yes");

    } 

    else {

        puts("No");

   }

 

   return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;

int main() {

    int A, B, C;

    while (cin >> A >> B >> C) {

        if ((A == B && A != C) || (A == C && A != B) || (B == C && B != A))

            cout << "Yes" << endl;

        else

            cout << "No" << endl;

    }



    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



 int main(void){

 	int a, b, c;

 	scanf("%d%d%d", &a, &b, &c);



    int ispoor = 0;

    if (a == b && b != c) ispoor = 1;

    if (b == c && c != a) ispoor = 1;

    if (c == a && a != b) ispoor = 1;



    if (ispoor) {

        puts("Yes");

    } 

    else {

        puts("No");

   }

 

   return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,b,c;

    scanf("%d%d%d",&a,&b,&c);

    if(a==b&&a!=c||b==c&&a!=c||a==c&&a!=b) printf("Yes");

    else printf("No");

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



 int main(void){

 	int a, b, c;

 	scanf("%d%d%d", &a, &b, &c);



    int ispoor = 0;

    if (a == b && b != c) ispoor = 1;

    if (b == c && c != a) ispoor = 1;

    if (c == a && a != b) ispoor = 1;



    if (ispoor) {

        puts("Yes");

    } 

    else {

        puts("No");

   }

 

   return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>

using namespace std;



int main(){

    int A, B, C;

    while(cin>>A>>B>>C){

        int t = 0;

        if(A==B)

            t++;

        if(A==C)

            t++;

        if(B==C)

            t++;

        if(t==1)

            cout << "Yes" << endl;

        else

            cout << "No" << endl;

    }

    return 0;

}




// Optimized solution to the same problem (faster):
#include <stdio.h>



 int main(void){

 	int a, b, c;

 	scanf("%d%d%d", &a, &b, &c);



    int ispoor = 0;

    if (a == b && b != c) ispoor = 1;

    if (b == c && c != a) ispoor = 1;

    if (c == a && a != b) ispoor = 1;



    if (ispoor) {

        puts("Yes");

    } 

    else {

        puts("No");

   }

 

   return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include <stdio.h>

using namespace std;

int main()

{

    char a[1000];

    char s[100000];

    int i=0;

    while(cin>>a)

    {

        getchar();

        s[i] = a[0];

        i++;

        if(a[0]=='C')

            if(a[1]=='o')

                if(a[2]=='n')

                    if(a[3]=='t')

                        if(a[4]=='e')

                            if(a[5]=='s')

                                if(a[6]=='t')

                    {

                        break;

                    }

    }

    int t=0;

    while(i--)

    {

        cout<<s[t];

        t++;

    }

    cout<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

char a[101];

char b[101];

char c[101];

int main()

{

	int i;

	scanf("%s",a);

	scanf("%s",b);

	scanf("%s",c);

	printf("%c%c%c",a[0],b[0],c[0]);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



int main()

{

	char s[301];

	scanf("%[^\n]", s);

	int len = strlen(s);

	int in;

	for(int i = 0; i < len; i++) {

		if(s[i] == ' ') {

			in = i;

			break;

		} 	

	}

	cout << "A" << s[in + 1] << "C";

	cout << endl;



	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

char a[101];

char b[101];

char c[101];

int main()

{

	int i;

	scanf("%s",a);

	scanf("%s",b);

	scanf("%s",c);

	printf("%c%c%c",a[0],b[0],c[0]);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <algorithm>

#include <string>



using namespace std;







char s[105];

int b = 0;







int main()

{

	while(~scanf("%s",&s)){

		cout << s[0];

		//memset(s, 0, sizeof(s));

	}

	cout << endl;





	system("pause");



}

// Optimized solution to the same problem (faster):
#include<stdio.h>

char a[101];

char b[101];

char c[101];

int main()

{

	int i;

	scanf("%s",a);

	scanf("%s",b);

	scanf("%s",c);

	printf("%c%c%c",a[0],b[0],c[0]);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

using namespace std;



string a, t;

int main() {

	cin >> t >> a >> t;

	cout << 'A' << a[0] << 'C';

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

char b;

int main() {

	scanf("%*s%s", &b);

	printf("A%cC", b);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

string s;

int main(){

	getline(cin,s);

	cout<<s[0];

	for(int i=1;i<s.size();i++)

	if(s[i]==' ')cout<<s[i+1];

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

string a,b,c;

int main(){

    cin>>a>>b>>c;

    cout<<a[0]<<b[0]<<c[0]<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;

int main()

{

	string a,b,c;

	cin>>a>>b>>c;

	return!(cout<<a[0]<<b[0]<<c[0]<<'\n');

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

     string a,b,c;

     cin>>a>>b>>c;

     cout<<a[0]<<b[0]<<c[0]<<endl;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;;

string a,b,c;

int main()

{

    cin>>a>>b>>c;

    cout<<a[0]<<b[0]<<c[0]<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    string a;

    for(int i=0;i<3;i++)

    {

    	cin>>a;

    	cout<<a[0];

    }

    cout<<endl;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main(){

	int a=1;

	bool f=true;

	while(a!=3){

		char b=getchar();

		if(f){

			putchar(b);

			f=false;

		}

		if(b==' '){

			b=getchar();

			putchar(b);

			a++;

		}

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

char a[10011001];

char b[10011001];

char c[10011001];



int main()

{

	scanf("%s %s %s",a,b,c);

	printf("%c%c%c\n",a[0],b[0],c[0]);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <math.h>

#include <string.h>

int main()

{

    char a[20000];

    scanf("%s",a);

        printf("%c",a[0]);

    scanf("%s",a);

        printf("%c",a[0]);

    scanf("%s",a);

        printf("%c\n",a[0]);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define rep(i,n)for(int i=0;i<(n);i++)

using namespace std;



int d[100], a[100];

int id[1000000];

vector<int>re[5040];

int dp[100][5040];

int h(vector<int>&v) {

	int p = 1, res = 0;

	for (int i : v)res += p*i, p *= 7;

	return res;

}

int main() {

	int n; scanf("%d", &n);

	rep(i, n)scanf("%d", &d[i]);

	rep(i, n)scanf("%d", &a[i]);

	vector<int>v(7); iota(v.begin(), v.end(), 0);

	int p = 0;

	do {

		id[h(v)] = p; re[p] = v;

		int cnt = 0;

		bool t[7]{};

		for (int i : v) {

			rep(j, 7) {

				if (t[j] && abs(j - i) <= d[j])cnt += a[j];

			}

			t[i] = 1;

		}

		dp[0][p] = cnt;

		p++;

	} while (next_permutation(v.begin(), v.end()));

	int ans = 0;

	for (int i = 1; i + 6 < n; i++)rep(j, 5040)rep(k, 8) {

		auto t = re[j].begin();

		int cnt = 0, o = 0;

		bool p[8]{};

		vector<int>q(7);

		rep(l, 8) {

			if (k == l) {

				rep(y, 8) {

					if (p[y] && y + d[i - 1 + y] >= 7)

						cnt += a[i - 1 + y];

				}

				p[7] = 1; q[o++] = 6;

			}

			else {

				if (p[7] && 7 - d[i + 6] <= *t)cnt += a[i + 6];

				p[*t] = 1;

				if (*t)q[o++] = *t - 1;

				t++;

			}

		}

		int v = id[h(q)];

		dp[i][v] = max(dp[i][v], dp[i - 1][j] + cnt);

		ans = max(ans, dp[i][v]);

	}

	printf("%d\n", ans);

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define rep(i,n)for(int i=0;i<(n);i++)

using namespace std;



int d[100], a[100], id[1000000], re[5040][7], dp[100][5040], v[7];

int h(int*v) {

	int p = 1, res = 0;

	rep(i, 7)res += v[i] * p, p *= 7;

	return res;

}

int main() {

	int n; scanf("%d", &n);

	rep(i, n)scanf("%d", &d[i]);

	rep(i, n)scanf("%d", &a[i]);

	iota(v, v + 7, 0);

	int p = 0;

	do {

		id[h(v)] = p; rep(i, 7)re[p][i] = v[i];

		int cnt = 0;

		bool t[7]{};

		for (int i : v) {

			rep(j, 7) {

				if (t[j] && abs(j - i) <= d[j])cnt += a[j];

			}

			t[i] = 1;

		}

		dp[0][p] = cnt;

		p++;

	} while (next_permutation(v, v + 7));

	int ans = 0;

	for (int i = 1; i + 6 < n; i++)rep(j, 5040)rep(k, 8) {

		int*t = re[j];

		int cnt = 0, o = 0;

		bool p[8]{};

		rep(l, 8) {

			if (k == l) {

				rep(y, 8) {

					if (p[y] && y + d[i - 1 + y] >= 7)

						cnt += a[i - 1 + y];

				}

				p[7] = 1; v[o++] = 6;

			}

			else {

				if (p[7] && 7 - d[i + 6] <= *t)cnt += a[i + 6];

				p[*t] = 1;

				if (*t)v[o++] = *t - 1;

				t++;

			}

		}

		int u = id[h(v)];

		dp[i][u] = max(dp[i][u], dp[i - 1][j] + cnt);

		ans = max(ans, dp[i][u]);

	}

	printf("%d\n", ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



const int N = (int)1e5 + 5;

const int M = (int)1e9 + 7;



ll powmod(ll a, ll b) {

  if(b == 0) return 1;

  ll ans = powmod(a, b / 2);

  ans = ans * ans % M;

  if(b % 2) ans = ans * a % M;

  return ans;

}



ll f[N], rf[N], n, a[N];



ll c(ll x, ll y) {

  return f[x] * rf[y] % M * rf[x - y] % M;

}



void build() {

  f[0] = 1;

  for(int i = 1 ; i < N ; ++i) f[i] = f[i - 1] * i % M;

  rf[N - 1] = powmod(f[N - 1], M - 2);

  for(int i = N - 2 ; i >= 0 ; --i) rf[i] = rf[i + 1] * (i + 1) % M;

}

void init() {

  cin >> n;

  for(int i = 1 ; i <= n ; ++i) cin >> a[i];

}

void solve() {

  int cc[3] = {0};

  ll ans = 1;

  for(int i = 1 ; i <= n ; ++i) {

    int ccc = 0, k = 0;

    for(int j = 2 ; j >= 0 ; --j) if(cc[j] == a[i]) {

      ccc++;

      k = j;

    }

    cc[k]++;

    ans = ans * ccc % M;

  }

  cout << ans << '\n';

  /*

  sort(cc, cc + 3);

  ans = 0;

  do {

    ans = (ans + (c(n, cc[0]) % M * c(n - cc[0], cc[1]) % M)) % M;

  } while(next_permutation(cc, cc + 3));

  cerr << cc[0] << ' ' << cc[1] << ' ' << cc[2] << '\n';

  cout << ans << '\n';

  */

}



int main() {

  ios_base::sync_with_stdio(0), cin.tie(0);

  build();

  init();

  solve();

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



const int N = (int)1e5 + 5;

const int M = (int)1e9 + 7;



ll n, a[N];



void init() {

  cin >> n;

  for(int i = 1 ; i <= n ; ++i) cin >> a[i];

}

void solve() {

  int cc[3] = {0};

  ll ans = 1;

  for(int i = 1 ; i <= n ; ++i) {

    int ccc = 0, k = 0;

    for(int j = 2 ; j >= 0 ; --j) if(cc[j] == a[i]) {

      ccc++;

      k = j;

    }

    cc[k]++;

    ans = ans * ccc % M;

  }

  cout << ans << '\n';

}



int main() {

  ios_base::sync_with_stdio(0), cin.tie(0);

  init();

  solve();

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<vector>



using namespace std;



typedef long long ll;



ll N, A[100000 + 10];



const ll MOD = 1000000007;



vector<ll> memo[100000 + 10];



int main() {

	cin >> N;

	for (int i = 0; i < N; i++)cin >> A[i];



	for (int i = 0; i <= N; i++)memo[i].resize(3, 0);

	ll ans = 1;

	for (int i = 0; i < N; i++) {

		if (count(memo[i].begin(), memo[i].end(), A[i]) == 0) {

			cout << 0 << endl;

			return 0;

		}

		ans *= count(memo[i].begin(), memo[i].end(), A[i]);

		ans %= MOD;

		memo[i + 1] = memo[i];

		int idx = find(memo[i + 1].begin(), memo[i + 1].end(), A[i]) - memo[i + 1].begin();

		memo[i + 1][idx]++;

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<queue>

#include<map>

#include<cassert>



using namespace std;



typedef long long ll;



int N;

ll A[100000 + 10];



const ll MOD = 1000000007;



int main() {

	cin >> N;

	for (int i = 0; i < N; i++)cin >> A[i];



	ll ans = 1;

	int cnt[3] = {};

	for (int i = 0; i < N; i++) {

		bool mode = false;

		ll times = 0;

		for (int j = 0; j < 3; j++) {

			if (!mode && A[i] == cnt[j]) {

				mode = true, cnt[j]++, times++;

			}

			else if (mode && A[i] == cnt[j]) {

				times++;

			}

		}

		(ans *= times) %= MOD;

	}

	cout << ans << endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <vector>

#include <string>

#include <queue>

#include <utility>

#include <map>

#include <bitset>

#include <numeric>

#define ll long long

using namespace std;

using p = pair<ll, ll>;

ll dx[8]={ 0, 1, 0,-1, 1, 1,-1,-1}; // x軸方向への変位

ll dy[8]={ 1, 0,-1, 0, 1,-1, 1,-1}; // y軸方向への変位



int main(void){

    ll n;

    cin >> n;

    ll INF = 1e+9+7;

    ll ans = 1;

    ll z = 3, y = 0;

    vector<ll> c(100010, 0);

    for(ll i = 1; i <= n; i++){

        ll a;

        cin >> a;

        if(a==0) ans = ans*(3-c[a])%INF;

        else ans = ans*(c[a-1]-c[a])%INF;

        c[a]++;

    }

    cout << ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <vector>

#include <string>

#include <queue>

#include <utility>

#include <map>

#include <bitset>

#include <numeric>

#define ll long long

using namespace std;

using p = pair<ll, ll>;

ll dx[8]={ 0, 1, 0,-1, 1, 1,-1,-1}; // x軸方向への変位

ll dy[8]={ 1, 0,-1, 0, 1,-1, 1,-1}; // y軸方向への変位



int main(void) {

    ll n;

    cin >> n;

    ll a[n];

    for(ll i = 0; i < n; i++)

        cin >> a[i];

    ll ans = 1;

    ll INF = 1e+9+7;

    vector<ll> c(n+1, 0);

    for(ll i = 0; i < n; i++){

        ll b = a[i];

        if(b==0) ans*=(3-c[b]);

        else ans*=(c[b-1]-c[b]);

        ans%=INF;

        c[b]++;

    }

    cout << ans << endl;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define F first

#define S second

#define pii pair<int, int>

#define eb emplace_back

#define all(v) v.begin(), v.end()

#define rep(i, n) for (int i = 0; i < (n); ++i)

#define rep3(i, l, n) for (int i = l; i < (n); ++i)

#define sz(v) (int)v.size()

#define inf (int)(1e9+7)

#define INF (ll)(1e18)

#define abs(x) (x >= 0 ? x : -(x))

#define ceil(a, b) a / b + !!(a % b)

template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }

template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }

template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }









// https://atcoder.jp/contests/sumitrust2019/submissions/8739342

// youtube解説の通り



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    

    int n;

    cin >> n;

    vector<int> a(n);

    rep(i, n) cin >> a[i];

    

    vector<ll> dp(100005);

    dp[0] = 1;

    int col = 3;

    vector<int> bc(n); // その個数のとこに何色あるか

    

    rep(i, n) {

        if (a[i] == 0) { // elseでa[i] - 1不可

            dp[i + 1] = dp[i] * col % inf;

            col--;

        } else {

            dp[i + 1] = dp[i] * bc[a[i] - 1] % inf;

            bc[a[i] - 1]--;

        }

        bc[a[i]]++;

    }

    cout << dp[n] << endl;

}



/*

 本質的な情報は何か

 

 最初

 0個 : 3色

 

 次 0

 0個 : 2色

 1個 : 1色

 

 次 1

 0個 : 2色

 1個 : 0色

 2個 : 1色

 

 

 */


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define F first

#define S second

#define pii pair<int, int>

#define eb emplace_back

#define all(v) v.begin(), v.end()

#define rep(i, n) for (int i = 0; i < (n); ++i)

#define rep3(i, l, n) for (int i = l; i < (n); ++i)

#define sz(v) (int)v.size()

#define inf (int)(1e9+7)

#define INF (ll)(1e18)

#define abs(x) (x >= 0 ? x : -(x))

#define ceil(a, b) a / b + !!(a % b)

template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }

template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }

template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }











int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    

    

    int table[100005] = {0};

    table[0] = 3;

    

    int n;

    cin >> n;

    

    ll ans = 1;

    rep(i, n) {

        int a;

        cin >> a;

        

        (ans *= table[a]) %= inf;

        table[a]--;

        table[a + 1]++;

    }

    

    cout << ans << endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

    for (int i=0; i<n; i++) {

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<ll> cur(1e5, 0);

    for (int i=0; i<n; i++) {

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1) % mod;

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

}


// Optimized solution to the same problem (faster):
#include <algorithm>

#include <climits>

#include <cmath>

#include <csignal>

#include <cstdio>

#include <cstdlib>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <string>

#include <tuple>

#include <vector>



using Graph = std::vector<std::vector<int>>;

using WGraph = std::vector<std::vector<std::pair<int, long long>>>;

using ll = long long;

using namespace std;



constexpr ll mod = 1000000007;

 

int main() {

    int n = 0;

    cin >> n;

    vector<int> a(n);

    for (int i=0; i<n; i++) {

        cin >> a.at(i);

    }

    

    int cnt0 = 0;

    for (int x : a) {

        if (x == 0) {

            cnt0++;

        }

    }

 

    if (cnt0 > 3) {

        cout << 0 << endl;

        return 0;

    }

 

    ll color = (cnt0 == 1) ? 3 : 6;

 

    ll ans = 1;

    vector<int> cur(1e5, 0);

    for (int i=0; i<n; i++) {

        if (a.at(i) == 0) {

            cur.at(0)++;

        } else {

            ans *= cur.at(a.at(i)-1);

            ans %= mod;

            cur.at(a.at(i))++;

            cur.at(a.at(i)-1)--;

        }

    }

    

    cout << ans * color % mod << endl;

 

    return 0;

}


// Fastest known solution for this problem: