#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long int llu;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define mem(a, v) memset(a, v, sizeof(a))
#define PI acos(-1)
#define S(a) scanf("%d",&a)
#define SL(a) scanf("%lld",&a)
#define S2(a, b) scanf("%d%d",&a,&b)
#define nl printf("\n")
#define deb(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x, y) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define deb3(x, y, z) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define debv(x) {cout<<#x<<" : "<<endl; for(int ii =0; ii < x.size(); ii++) cout<<x[ii]<<" "; cout<<endl; }
#define debarr(x, xs) {cout<<#x<<" : "<<endl; for(int ii =0; ii < xs; ii++) cout<<x[ii]<<" "; cout<<endl; }
//auto T=clock();
//cout<<double(clock()-T)/CLOCKS_PER_SEC<<'\n';
const ll mod = 1000000007LL;
const int lmt = 1000005;
int cnt[lmt];
int n, N = 0;
ll dp[lmt][2][2][2];
ll powr(int x, int y) {
if(y == 0)
return 1LL;
ll tmp = powr(x, y/2);
tmp = (tmp*tmp) % mod;
if(y&1)
tmp = (tmp*x) % mod;
return tmp;
}
ll solve(int idx, int ab, int bc, int ac) {
if(idx > N) {
if((ab == 1) && (bc == 1) && (ac == 1))
return 1;
return 0;
}
if(cnt[idx] == 0)
return solve(idx+1, ab, bc, ac);
if(dp[idx][ab][bc][ac] != -1)
return dp[idx][ab][bc][ac];
ll ans = solve(idx+1, ab, bc, ac);
ans %= mod;
ll mul = cnt[idx];
ll mul1 = (3LL*mul) % mod;
ans += ((mul1*solve(idx+1, ab, bc, ac)) % mod);
ans %= mod;
ll mul2 = (mul*mul) % mod;
ans += ((mul2*solve(idx+1, 1, bc, ac)) % mod);
ans %= mod;
ans += ((mul2*solve(idx+1, ab, 1, ac)) % mod);
ans %= mod;
ans += ((mul2*solve(idx+1, ab, bc, 1)) % mod);
ans %= mod;
return dp[idx][ab][bc][ac] = ans;
}
int main(){
mem(cnt, 0);
mem(dp, -1);
S(n);
for(int i = 0; i < n; i++) {
int x;
S(x);
N = max(N, x);
cnt[x]++;
}
ll ans = solve(1, 0, 0, 0);
printf("%lld\n", ans);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgaW50IGxsdTsKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBYIGZpcnN0CiNkZWZpbmUgWSBzZWNvbmQKI2RlZmluZSBtZW0oYSwgdikgbWVtc2V0KGEsIHYsIHNpemVvZihhKSkKI2RlZmluZSBQSSBhY29zKC0xKQojZGVmaW5lIFMoYSkgc2NhbmYoIiVkIiwmYSkKI2RlZmluZSBTTChhKSBzY2FuZigiJWxsZCIsJmEpCiNkZWZpbmUgUzIoYSwgYikgc2NhbmYoIiVkJWQiLCZhLCZiKQojZGVmaW5lIG5sIHByaW50ZigiXG4iKQojZGVmaW5lIGRlYih4KSBjb3V0PDwjeDw8IiA6ICI8PHg8PGVuZGw7CiNkZWZpbmUgZGViMih4LCB5KSBjb3V0PDwjeDw8IiA6ICI8PHg8PCIgfCAiPDwjeTw8IiA6ICI8PHk8PGVuZGw7CiNkZWZpbmUgZGViMyh4LCB5LCB6KSBjb3V0PDwjeDw8IiA6ICI8PHg8PCIgfCAiPDwjeTw8IiA6ICI8PHk8PCIgfCAiPDwjejw8IiA6ICI8PHo8PGVuZGw7CiNkZWZpbmUgZGVidih4KSB7Y291dDw8I3g8PCIgOiAiPDxlbmRsOyBmb3IoaW50IGlpID0wOyBpaSA8IHguc2l6ZSgpOyBpaSsrKSBjb3V0PDx4W2lpXTw8IiAiOyBjb3V0PDxlbmRsOyB9CiNkZWZpbmUg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