/*
* Author: sparks
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define pll pair<ll, ll>
#define rep(i,n) for(i=0;i<n;i++)
#define all(v) v.begin(), v.end()
void inp() {}
template <typename T, typename... Args>
void inp(T& n, Args&... args) //fast input function
{
int ch=getchar(), sign=1; n=0;
while( ch < '0' || ch > '9' ) { if(ch=='-')sign=-1; ch=getchar(); }
while( ch >= '0' && ch <= '9' ) n = (n<<3)+(n<<1) + ch-'0', ch=getchar();
n=n*sign; inp(args...);
}
#define MOD 1000000007
int main()
{
ll i, n; inp(n);
vector<ll> v;
map<ll, ll> m;
rep(i, n)
{
ll t; inp(t);
if(m.find(t) == m.end())
{
v.pb(0);
m[t] = v.size() - 1;
}
v[m[t]] += 1;
}
ll x, y, z, e;
x = y = z = e = 1;
rep(i, v.size())
{
ll t = 1;
t += (3 * v[i]) % MOD;
t %= MOD;
t += (3 * ((v[i] * v[i]) % MOD)) % MOD;
t %= MOD;
x *= t;
x %= MOD;
}
rep(i, v.size())
{
ll t = 1;
t += (3 * v[i]) % MOD;
t %= MOD;
t += (2 * ((v[i] * v[i]) % MOD)) % MOD;
t %= MOD;
y *= t;
y %= MOD;
}
y *= 3;
y %= MOD;
rep(i, v.size())
{
ll t = 1;
t += (3 * v[i]) % MOD;
t %= MOD;
t += (1 * ((v[i] * v[i]) % MOD)) % MOD;
t %= MOD;
e *= t;
e %= MOD;
}
e *= 3;
e %= MOD;
rep(i, v.size())
{
ll t = 1;
t += (3 * v[i]) % MOD;
t %= MOD;
z *= t;
z %= MOD;
}
// cerr << x << ' ' << y << ' ' << z << endl;
ll ans = 0;
ans += x; ans %= MOD;
ans -= y; ans += MOD; ans %= MOD;
ans += e; ans %= MOD;
ans -= z; ans += MOD; ans %= MOD;
cout << ans << endl;
return 0;
}
LyoKICogICAgICBBdXRob3I6IHNwYXJrcwogKi8KI2luY2x1ZGUgPGJpdHMvc3R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