#include<bits/stdc++.h>
using namespace std;
const int BS=1e5+5;
char buf[BS],*S,*T;
inline char gc(){
	if(S==T)T=(S=buf)+fread(buf,1,BS,stdin);
	return S!=T?*(S++):EOF;
}
inline int in(){
	int x=0,f=1;char c=gc();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc();}
	while(c>='0'&&c<='9')x=x*10+c-48,c=gc();
	return x*f;
}
const int N=2e5+5,mod=998244353;
int n,a[N],cnt,ans=1,d[N];
bool vis[N],mark[N];
stack<int> s;
int main(){
    n=in();
    for(int i=1;i<=n;i++)a[i]=in(),d[a[i]]++;
    for(int i=1;i<=n;i++){
        int p=i;
        while(!vis[p])vis[p]=1,mark[p]=1,s.push(p),p=a[p];
        if(mark[p])ans=(ans+ans)%mod;
        while(s.size())mark[s.top()]=0,s.pop();
    }
    cout<<(ans+mod-1)%mod;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1<<24;
const ll INFL = (1ll << 60);
//cout << setprecision(20) << 

int n;
int coord[17][3];
int dp[1 << 17][17];

int rec(int S, int v) {
    if (dp[S][v] >= 0) {
        return dp[S][v];
    }

    if (S == (1 << n) - 1 && v == 0) {
        return dp[S][v] = 0;
    }

    int res = INF;
    for (int u = 0; u < n; u++) {
        if (!(S >> u & 1)) {
            int a, b, c, p, q, r;
            a = coord[v][0];
            b = coord[v][1];
            c = coord[v][2];
            p = coord[u][0];
            q = coord[u][1];
            r = coord[u][2];
            int dist = abs(a - p) + abs(b - q) + max(r - c, (int)0);
            res = min(res, rec(S | 1 << u, u) + dist);
        }
    }
    return dp[S][v] = res;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        ll x, y, z; cin >> x >> y >> z;
        coord[i][0] = x;
        coord[i][1] = y;
        coord[i][2] = z;
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0) << endl;
}