#include<bits/stdc++.h>
#define R register
#define CN const
#define int long long
#define rint R int
#define cint CN int
#define sib 1048576
char buf[sib],*p1,*p2;
#define uc (p1==p2&&(p2=(p1=buf)+fread(buf,1,sib,stdin),p1==p2)?0:*p1++)
char pbuf[sib],*pp=pbuf;
inline void ph(const char s) {
	if(pp-pbuf==sib)fwrite(pbuf,1,sib,stdout),pp=pbuf;
	*pp++=s;
}
//#define uc getchar()
//#define ph putchar
#define ps ph(' ')
#define pe ph('\n')
using namespace std;
cint inf=998244353;
cint mix=1e6+5;
inline int in();
inline void out(rint x);
int n=in(),ans=0;
int fa[mix],ru[mix];
int que[mix],ft,bk;
bool vis[mix];
inline int ksm(rint x,rint y){
	rint rt=1;
	while(y){
		if(y&1)(rt*=x)%=inf;
		(x*=x)%=inf,y>>=1;
	}	return rt;
}
#undef int
int main() {
#define int long long
	for(rint i=1;i<=n;++i)++ru[fa[i]=in()];
	for(rint i=1;i<=n;++i)
	if(!ru[i])que[++bk]=i;
	while(ft<bk){
		rint x=que[++ft];
		vis[x]=1;
		--ru[fa[x]];
		if(!ru[fa[x]])
		que[++bk]=fa[x];
	}	for(rint i=1;i<=n;++i)
	if(!vis[i]){
		rint x=i;
		while(!vis[x])
		vis[x]=1,x=fa[x];
		++ans;
	}	out((ksm(2,ans)+inf-1)%inf),pe;
	fwrite(pbuf,1,pp-pbuf,stdout);
}
inline int in() {
	rint x=0;
	R bool f=0;
	R char s=uc;
	if(!s)return 0;
	while((s<'0'||'9'<s)&&s!='-')s=uc;
	if(s=='-')f=1,s=uc;
	while('0'<=s&&s<='9')(x*=10)+=s^48,s=uc;
	return f?-x:x;
}
inline void out(rint x) {
	if(x<0)ph('-'),x=-x;
	rint y=x/10;
	if(y)out(y);
	ph((x-y*10)^48);
}
/*
Solved by sid2104
on 11/11/2020  22:32 
*/
#include <bits/stdc++.h>
using namespace std;
#define int         long long
#define float       long double
#define pb          push_back
#define ld          long double
#define f(i,a,b)    for(int i=a;i<b;i++)
#define mp          make_pair
#define pii         pair<int,int>
#define vi          vector<int>
#define vpii         vector<pair<int,int> >
#define mii         map<int,int>
#define all(a)      (a).begin(),(a).end()
#define debug(x)    cout<<#x<<" is "<<(x)<<endl;cout.flush();
//#define debug(x)    ;
//#define debug(x)    ;
// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
    os<<"[";
    for(auto& x:v){os<<x<<", ";}
    return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
    return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===
#define mp          make_pair
#define x           first
#define y           second
#define pb          push_back
#define endl        '\n'
#define si(x)       (int)((x).size())
#define IOS         ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define txt         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
#define tree        vector<vector<int> >
#define PI          3.14159265358979323846
int inf=99999999999;
const int mod=1e9+7;
const int cmp=1e17;
inline int gcd(int a, int b) {
  while (a > 0 && b > 0) {
    if (a > b) a %= b;
    else b %= a;
  }
  return a + b;
}
void inp(int n,int arr[])
{
    f(i,0,n) cin>>arr[i];
}
int ans=0;
void solve()
{
  int sum=0;
  int curr=0;
  int currm=0;
  int n;
  cin>>n;
   f(i,0,n)
   {
     int k;
     cin>>k;
     curr+=k;
 
     currm=max(curr,currm);
     ans=max(ans,sum+currm);
     sum+=curr;
    
   }
   cout<<ans<<endl;
        
}
main()
{
    IOS;
    solve();
  

}








