#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct comand {
	int data;
	char ch;
};

int main()
{
	int i, j, n, *arr, *summ, max, before = 0;
	//const char *initials = "ABBABAABBB";
	cin >> n;
	char *initials = (char *)malloc(sizeof(initials) * n);
	arr = (int *)calloc(sizeof(arr), n);
	for (i = 0; i < n; i++)
		cin >> arr[i];
	cin >> initials;
	summ = (int *)calloc(sizeof(summ), n);
	struct comand **comand = (struct comand**)malloc(sizeof(comand) * n);
	for (i = 0; i < n; i++) {
		comand[i] = (struct comand*)malloc(sizeof(comand[i]));
		comand[i]->data = arr[i];
		comand[i]->ch = initials[i];
		if (initials[i] == 'B')
			before += arr[i];
	}
	//comand = selectSort(comand, n);
	for (i = 0; i < n; i++)
		printf("%d ", comand[i]->data);
	for (i = 0; i < n; i++)
		printf("%c ", comand[i]->ch);
	summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
	for (max = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > max)
			max = summ[i];
	}
	if (max > 0)
		printf("\n%d\n", max + before);
	else
		printf("%d\n", before);
	for (i = 0; i < n; i++) {
		printf("%d ", summ[i]);
		free(comand[i]);
	}
	free(comand);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

struct comand {
    long long int data;
    char ch;
};

int main()
{
    int i, j, n;
    long long int *arr, *summ, suffmax, before = 0, prefmax;
    cin >> n;
    char *initials = new char [n];
    arr = new long long int [n];
    for (i = 0; i < n; i++)
	cin >> arr[i];
    cin >> initials;
    summ = new long long int [n];
    struct comand **comand = new struct comand* [n];
    for (i = 0; i < n; i++) {
	comand[i] = new struct comand;
	comand[i]->data = arr[i];
	comand[i]->ch = initials[i];
	if (initials[i] == 'B')
	    before += arr[i];
    }
    summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
    for (suffmax = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
	if (comand[j]->ch == 'A')
	    summ[i] = comand[j]->data + summ[i - 1];
	else
	    summ[i] = summ[i - 1] - comand[j]->data;
	if (summ[i] > suffmax)
	    suffmax = summ[i];
    }
    summ[0] = (comand[0]->ch == 'A' ? comand[0]->data : -(comand[0]->data));
    for (prefmax = summ[0], i = 1, j = 1; i < n; i++, j++) {
	if (comand[j]->ch == 'A')
	    summ[i] = comand[j]->data + summ[i - 1];
	else
	    summ[i] = summ[i - 1] - comand[j]->data;
	if (summ[i] > prefmax)
	    prefmax = summ[i];
    }
    if (prefmax != suffmax)
	prefmax = max(prefmax, suffmax);
    if (prefmax > 0)
	printf("%lld", prefmax + before);
    else
	printf("%lld", before);
    for (i = 0; i < n; i++) {
	delete[](comand[i]);
    }
    delete[](comand);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct comand {
	int data;
	char ch;
};

struct comand **selectSort(struct comand **arr, int size)
{
	int i, j, minValueIndex;
	struct comand  *tmp = (struct comand *)malloc(sizeof(comand));
	for(i = 0; i < size - 1; i++) {
		minValueIndex = i;
		for (j = i + 1; j < size; j++) {
			if (arr[minValueIndex]->data > arr[j]->data) {
				minValueIndex = j;
			}
			//if(arr[j]->data < arr[minValueIndex]->data)
			//	minValueIndex = j;
		}
		if (minValueIndex != i) {
			tmp = arr[i];
			arr[i] = arr[minValueIndex];
			arr[minValueIndex] = tmp;
		}
	}
	return arr;
}

int main()
{
	int i, j, n, *arr, *summ, max, before = 0;
	cin >> n;
	char *initials = (char *)malloc(sizeof(initials) * n);
	arr = (int *)calloc(sizeof(arr), n);
	for (i = 0; i < n; i++)
		cin >> arr[i];
	cin >> initials;
	summ = (int *)calloc(sizeof(summ), n);
	struct comand **comand = (struct comand**)malloc(sizeof(comand) * n);
	for (i = 0; i < n; i++) {
		comand[i] = (struct comand*)malloc(sizeof(comand[i]));
		comand[i]->data = arr[i];
		comand[i]->ch = initials[i];
		if (initials[i] == 'B')
			before += arr[i];
	}
	comand = selectSort(comand, n);
	/*for (i = 0; i < n; i++)
		printf("%d ", comand[i]->data);
	for (i = 0; i < n; i++)
		printf("%c ", comand[i]->ch);*/
	summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : 0);
	for (max = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > max)
			max = summ[i];
	}
	if (max > 0)
		printf("%d", max + before);
	else
		printf("%d", before);
	for (i = 0; i < n; i++) {
		//printf("%d ", summ[i]);
		free(comand[i]);
	}
	free(comand);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 1e6 + 5;

int a[N], n;
ll prea[N], suffa[N], preb[N], suffb[N];
char c[N];

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
	}

	FOR(i, 1, n) {
	 	scanf(" %c", &c[i]);
		prea[i] = prea[i-1] + (c[i] == 'A') * a[i];
		preb[i] = preb[i-1] + (c[i] == 'B') * a[i];
	}

	ROF(i, n, 1) {
		suffa[i] = suffa[i+1] + (c[i] == 'A') * a[i];
		suffb[i] = suffb[i+1] + (c[i] == 'B') * a[i];
	}

	ll ans = 0;

	FOR(i, 0, n) {
		ans = max(ans, prea[i] + suffb[i+1]);
		ans = max(ans, preb[i] + suffa[i+1]);
	}

	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 1e6 + 5;

int a[N], n;
ll prea[N], suffa[N], preb[N], suffb[N];
char c[N];

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
	}

	FOR(i, 1, n) {
	 	scanf(" %c", &c[i]);
		prea[i] = prea[i-1] + (c[i] == 'A') * a[i];
		preb[i] = preb[i-1] + (c[i] == 'B') * a[i];
	}

	ROF(i, n, 1) {
		suffa[i] = suffa[i-1] + (c[i] == 'A') * a[i];
		suffb[i] = suffb[i-1] + (c[i] == 'B') * a[i];
	}

	ll ans = 0;

	FOR(i, 0, n) {
		ans = max(ans, prea[i] + suffb[i+1]);
		ans = max(ans, preb[i] + suffa[i+1]);
	}

	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int a[N], n;
ll prea[N], suffa[N], preb[N], suffb[N];
char c[N];

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
	}

	FOR(i, 1, n) {
	 	scanf(" %c", &c[i]);
		prea[i] = prea[i-1] + (c[i] == 'A') * a[i];
		preb[i] = preb[i-1] + (c[i] == 'B') * a[i];
	}

	ROF(i, n, 1) {
		suffa[i] = suffa[i-1] + (c[i] == 'A') * a[i];
		suffb[i] = suffb[i-1] + (c[i] == 'B') * a[i];
	}

	ll ans = 0;

	FOR(i, 0, n) {
		ans = max(ans, prea[i] + suffb[i+1]);
		ans = max(ans, preb[i] + suffa[i+1]);
	}

	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,i;
	scanf("%ld",&n);
	long long int ar[n];
	for(i=0;i<n;i++)
	{
		cin>>ar[i];
	}
	char str[n];
	scanf("%s",str);
	long long int dp[n],a=0,b=0,max=0,a1,b1,max1=0,b2;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
			a+=ar[i];
		else
			b+=ar[i];
	}
	a1=a;
	b1=b;
	b2=b;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
		{
			b=b+ar[i];
			a=a-ar[i];
			if(b>max)
				max=b;
		}
		else
		{
			b=b-ar[i];
			a=a+ar[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		if(str[i]=='A')
		{
			b1=b1+ar[i];
			a1=a1-ar[i];
			if(b1>max1)
				max1=b1;
		}
		else
		{
			b1=b1-ar[i];
			a1=a1+ar[i];
		}
	}	if(max1>max&&max1>b2)
		cout<<max1<<endl;
	else if(max>b2)
		cout<<max<<endl;
	else
		cout<<b2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,i;
	scanf("%ld",&n);
	long long int ar[n];
	for(i=0;i<n;i++)
	{
		cin>>ar[i];
	}
	char str[n];
	scanf("%s",str);
	long long int dp[n],a=0,b=0,max=0,a1,b1,max1=0,b2;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
			a+=ar[i];
		else
			b+=ar[i];
	}
	a1=a;
	b1=b;
	b2=b;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
		{
			b=b+ar[i];
			a=a-ar[i];
			if(b>max)
				max;
		}
		else
		{
			b=b-ar[i];
			a=a+ar[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		if(str[i]=='A')
		{
			b1=b1+ar[i];
			a1=a1-ar[i];
			if(b1>max1)
				max1=b1;
		}
		else
		{
			b1=b1-ar[i];
			a1=a1+ar[i];
		}
	}
	if(max1>max&&max>b2)
		cout<<max1<<endl;
	else if(max>b2)
		cout<<max<<endl;
	else
		cout<<b2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,i;
	scanf("%ld",&n);
	long long int ar[n];
	for(i=0;i<n;i++)
	{
		cin>>ar[i];
	}
	char str[n];
	scanf("%s",str);
	long long int dp[n],a=0,b=0,max=0,a1,b1,max1=0,b2;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
			a+=ar[i];
		else
			b+=ar[i];
	}
	a1=a;
	b1=b;
	b2=b;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
		{
			b=b+ar[i];
			a=a-ar[i];
			if(b>max)
				max;
		}
		else
		{
			b=b-ar[i];
			a=a+ar[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		if(str[i]=='A')
		{
			b1=b1+ar[i];
			a1=a1-ar[i];
			if(b1>max1)
				max1=b1;
		}
		else
		{
			b1=b1-ar[i];
			a1=a1+ar[i];
		}
	}
	if(max1>max&&max1>b2)
		cout<<max1<<endl;
	else if(max>b2)
		cout<<max<<endl;
	else
		cout<<b2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,i;
	scanf("%ld",&n);
	long long int ar[n];
	for(i=0;i<n;i++)
	{
		cin>>ar[i];
	}
	char str[n+50];
	scanf("%s",str);
	long long int dp[n],a=0,b=0,max=0,a1,b1,max1=0,b2;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
			a+=ar[i];
		else
			b+=ar[i];
	}
	a1=a;
	b1=b;
	b2=b;
	for(i=0;i<n;i++)
	{
		if(str[i]=='A')
		{
			b=b+ar[i];
			a=a-ar[i];
			if(b>max)
				max=b;
		}
		else
		{
			b=b-ar[i];
			a=a+ar[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		if(str[i]=='A')
		{
			b1=b1+ar[i];
			a1=a1-ar[i];
			if(b1>max1)
				max1=b1;
		}
		else
		{
			b1=b1-ar[i];
			a1=a1+ar[i];
		}
	}	if(max1>max&&max1>b2)
		cout<<max1<<endl;
	else if(max>b2)
		cout<<max<<endl;
	else
		cout<<b2<<endl;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
     long long int t,max=0,sum=0,f;
cin>>t;
    int a[t];
    char b[t];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;
       max=0;
        for(int i=0;i<t;i++)
        {

            if(b[i]=='B')
                {max=max+a[i];}

        }
        //cout<<max<<"\n";
        f=sum=max;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum>max)
            max=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum >max)
            max=sum;

        }
        cout<<max;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
     long long int t,max=0,sum=0,f;
cin>>t;
    int a[t];
    char b[t];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;
       max=0;
        for(int i=0;i<t;i++)
        {

            if(b[i]=='B')
                {max=max+a[i];}

        }
        //cout<<max<<"\n";
        f=max;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum>max)
            max=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum >max)
            max=sum;

        }
        cout<<max;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
     long long int t,max=0,sum=0,f;
cin>>t;
    long long int a[t];
    char b[t];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;
       max=0;
        for(int i=0;i<t;i++)
        {

            if(b[i]=='B')
                {max=max+a[i];}

        }
        //cout<<max<<"\n";
        f=sum=max;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum>max)
            max=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum >max)
            max=sum;

        }
        cout<<max;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
     long long int t,max=0,sum=0,f;
cin>>t;
    int a[t];
    char b[t];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;
       max=0;
        for(int i=0;i<t;i++)
        {

            if(b[i]=='B')
                {max=max+a[i];}

        }
        //cout<<max<<"\n";
        f=max;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum>max)
            max=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum >max)
            max=sum;

        }
        cout<<max<<"\n";
    return 0;
}


#include <iostream>

using namespace std;
 long long int t,amax=0,sum=0,f;
long long int a[1000000]={0};

int main()
{

    cin>>t;
    char b[t+5];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;

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

            if(b[i]=='B')
                {amax=amax+a[i];}

        }
        //cout<<max<<"\n";
        f=sum=amax;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum=sum+a[i];
        else
            sum=sum-a[i];
        if(sum > amax)
            amax=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum > amax)
            amax=sum;

        }
        cout<<amax;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
     lomg long int t,max=0,sum=0,f;
cin>>t;
    int a[t];
    char b[t];
    for(int i=0;i<t;i++)
        {cin>>a[i];}
        cin>>b;
       // cout<<b;
       max=0;
        for(int i=0;i<t;i++)
        {

            if(b[i]=='B')
                {max=max+a[i];}

        }
        //cout<<max<<"\n";
        f=max;
        for(int i=0;i<t;i++)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum>max)
            max=sum;

        }
        sum=f;
         for(int i=t-1;i>=0;i--)
        {
        if(b[i]=='A')
            sum+=a[i];
        else
            sum-=a[i];
        if(sum >max)
            max=sum;

        }
        cout<<max<<"\n";
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
LL arr[512345];
char str[512345];

int main()
{
    LL n;
    scanf("%lld",&n);
    for(LL i=0;i<n;i++)
    {
        scanf("%lld",&arr[i]);
    }
    scanf("%s",str);
    LL sum=0;
    for(LL i=0;i<n;i++)
    {
        if('B'==str[i]){
            sum+=arr[i];
        }
    }
    LL he;
    he=sum;
    LL ans=he;
    for(LL i=0;i<n;i++)
    {
        if(str[i]=='A'){
            he+=arr[i];
        }else{
            he-=arr[i];
        }
        ans=max(ans,he);
    }
    he=sum;
    for(LL i=n-1;i>=0;i--)
    {
        if(str[i]=='A'){
            he+=arr[i];
        }else{
            he-=arr[i];
        }
        ans=max(ans,he);
    }
    printf("%lld",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int p[500010];
int main()
{
    int n;
    string s;
    long long sum,ans;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>p[i];
        }
        sum = 0;
        cin>>s;
        for(int i=0;i<=n-1;i++)
        {
            if(s[i]=='B'){sum = sum + p[i+1];}
        }
        ans = sum;
        long long t = sum;
         for(int i=1;i<=n;i++)
         {
             if(s[i-1]=='A'){sum = sum + p[i];}
             else{sum = sum - p[i];}
             if(sum>ans)
             {
                 ans = sum;
             }
         }
         sum = t;
          for(int i=n;i>=1;i--)
         {
             if(s[i-1]=='A'){sum = sum + p[i];}
             else{sum = sum - p[i];}
             if(sum>ans)
             {
                 ans = sum;
             }
         }
         cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
long long a[600000];
int main()
{
	string s;
	long long minl=0,minr=0,sum=0,flagl=0,flagr=0;
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		scanf("%I64d",&a[i]);
	}
	cin>>s;
	int len=s.length();
	for(int i=0;i<len;i++)
	{
		if(s[i]=='A')
		{
			flagl-=a[i];
		}
		else if(s[i]=='B')
		{
			flagl+=a[i];
			sum+=a[i];
		}
		if(flagl<0&&minl>flagl)
		{
			minl=flagl;
		}
	}
	for(int i=s.length()-1;i>=0;i--)
	{
		if(s[i]=='A')
		{
			flagr-=a[i];
		}
		else if(s[i]=='B')
		{
			flagr+=a[i];
		}
		if(flagr<0&&minr>flagr)
		{
			minr=flagr;
		}
	}
	cout<<sum-min(minl,minr)<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[600001];
ll pre[600001];
int main()
{
    cin.sync_with_stdio(false);
   ll ans = 0;
   int n;
   cin >> n;
    pre[0] = 0;
   for( int i = 1 ; i  <= n ; ++i )
       cin >> a[i];
   string x;
   cin >> x;
   for( int i = 1 ; i <= n ; ++i ){
    pre[i] = pre[i-1];
    if(x[i-1] == 'A')
         pre[i] += a[i];
   }
   ans = pre[n];
   ll sm = 0;
   for(int i = 1 ; i <= n ; ++i ){
         sm += a[i];
       ans = max( ans , pre[n] + sm - 2*pre[i]);
   }
   sm = 0;
   for(int i = n ; i >= 1 ; --i ){
     sm += a[i];
     ans = max( ans , 2*pre[i-1] + sm - pre[n] );
   }
   cout << ans << endl;

}


#include<bits/stdc++.h>
using namespace std;
# define ll long long
ll alice[3][511111],bob[3][511111],ar[511111];
int main()
{
    cin.sync_with_stdio(false);
    ll ans=0;
    int n,i,j;
    string str;
    char ch;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>ar[i];
    cin>>str;
    alice[0][0]=alice[1][n+1]=bob[0][0]=bob[1][n+1]=0;
    for(i=1;i<=n;i++)
    {
        alice[0][i]=alice[0][i-1];
        bob[0][i]=bob[0][i-1];
        ch=str[i-1];
        if(ch=='A')
            alice[0][i]+=ar[i];
        else
            bob[0][i]+=ar[i];
    }
    for(i=n;i>=1;i--)
    {
        alice[1][i]=alice[1][i+1];
        bob[1][i]=bob[1][i+1];
        ch=str[i-1];
        if(ch=='A')
            alice[1][i]+=ar[i];
        else
            bob[1][i]+=ar[i];
    }
    for(i=1;i<=n;i++)
    {
        ans=max(alice[0][i]+bob[1][i],ans);
        ans=max(alice[1][i]+bob[0][i],ans);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[600001];
ll pre[600001];
int main()
{
   ll ans = 0;
   int n;
   cin >> n;
    pre[0] = 0;
   for( int i = 1 ; i  <= n ; ++i )
       cin >> a[i];
   string x;
   cin >> x;
   for( int i = 1 ; i <= n ; ++i ){
    pre[i] = pre[i-1];
    if(x[i-1] == 'A')
         pre[i] += a[i];
   }
   ans = pre[n];
   ll sm = 0;
   for(int i = 1 ; i <= n ; ++i ){
         sm += a[i];
       ans = max( ans , pre[n] + sm - 2*pre[i]);
   }
   sm = 0;
   for(int i = n ; i >= 1 ; --i ){
     sm += a[i];
     ans = max( ans , 2*pre[i-1] + sm - pre[n] );
   }
   cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;

#define N 500005
#define ll long long
char s[N];
int arr[N];
ll r[N];
ll l[N];
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i<n; i++)
	{
		scanf("%d", &arr[i]);
	}
	scanf("%s", s);

	r[n] = 0;
	ll maxm = 0;
	ll ct = 0;
	for(int i = n-1; i>=0; i--)
	{
		if(s[i]=='A')
		{
			r[i] = r[i+1]+(ll)arr[i];
		}
		else
		{
			ct+=(ll)arr[i];
			r[i] = r[i+1]-(ll)arr[i];
		}
		if(r[i]>maxm)
			maxm = r[i];
	}

	if(s[0]=='A')
		l[0] = arr[0];
	else
		l[0] = -arr[0];
	for(int i = 1; i<n; i++)
	{
		if(s[i]=='A')
		{
			l[i] = l[i-1]+(ll)arr[i];
		}
		else
		{
			l[i] = l[i-1]-(ll)arr[i];
		}
		if(l[i]>maxm)
			maxm = l[i];
	}

	cout << ct+maxm << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 1234567
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=diff[i];
		if(max_ending_here<0)
			max_ending_here = 0;
		if(max_ending_here>max_score_tot)
			max_score_tot = max_ending_here;
	}

	score+=max_score_tot;
	cout << score << endl;

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 700000
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=(ll)p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	int prev = 0;
	int last = 0;
	int fprev = 0;
	int flast = -1;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
		{
			max_ending_here = 0;
			prev = i+1;
			last = prev-1;
		}
		if(max_ending_here>max_score_tot)
		{
			max_score_tot = max_ending_here;
			flast = last;
			fprev = prev;
		}
		last++;
	}

	// cout << fprev << " " << flast << endl;
	// cout << max_score_tot << endl;
	score+=max_score_tot;
	cout << score << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define N 100005
#define ll long long
char s[N];
ll arr[N];
ll r[N];
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i<n; i++)
	{
		cin >> arr[i];
	}
	scanf("%s", s);

	r[n] = 0;
	ll maxm = 0;
	ll ct = 0;
	for(int i = n-1; i>=0; i--)
	{
		if(s[i]=='A')
		{
			r[i] = r[i+1]+arr[i];
		}
		else
		{
			ct+=arr[i];
			r[i] = r[i+1]-arr[i];
		}
		if(r[i]>maxm)
			maxm = r[i];
	}

	cout << ct+maxm << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define N 500005
#define ll long long
char s[N];
int arr[N];
ll r[N];
ll l[N];
int main()
{
	int n;
	cin >> n;
	for(int i = 0; i<n; i++)
	{
		scanf("%d", &arr[i]);
	}
	scanf("%s", s);

	r[n] = 0;
	ll maxm = 0;
	ll ct = 0;
	for(int i = n-1; i>=0; i--)
	{
		if(s[i]=='A')
		{
			r[i] = r[i+1]+(ll)arr[i];
		}
		else
		{
			ct+=(ll)arr[i];
			r[i] = r[i+1]-(ll)arr[i];
		}
		if(r[i]>maxm)
			maxm = r[i];
	}

	for(int i = 0; i<n; i++)
	{
		if(s[i]=='A')
		{
			if(i==0)
				l[0] = arr[0];
			else
				l[i] = l[i-1]+(ll)arr[i];
		}
		else
		{
			if(i==0)
				l[0] = -arr[0];
			else
				l[i] = l[i-1]-(ll)arr[i];
		}
		if(l[i]>maxm)
			maxm = l[i];
	}

	ll ans = ct+maxm;
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 700000
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=(ll)p[i];
			diff[i] = -2*p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	int prev = 0;
	int last = 0;
	int fprev = 0;
	int flast = -1;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
		{
			max_ending_here = 0;
			prev = i+1;
			last = prev-1;
		}
		if(max_ending_here>max_score_tot)
		{
			max_score_tot = max_ending_here;
			flast = last;
			fprev = prev;
		}
		last++;
	}

	// cout << fprev << " " << flast << endl;
	// cout << max_score_tot << endl;
	score+=max_score_tot;
	cout << score << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 700000
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=(ll)p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	int prev = 0;
	int last = 0;
	int fprev = 0;
	int flast = 0;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
		{
			max_ending_here = 0;
			prev = i+1;
			last = prev-1;
		}
		if(max_ending_here>max_score_tot)
		{
			max_score_tot = max_ending_here;
			flast = last;
			fprev = prev;
		}
		last++;
	}

	int j = 0;
	while(j<fprev)
	{
		if(s[j]=='B')
			max_score_tot+=(ll)p[j];
		j++;
	}

	cout << max_score_tot << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 1234567
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=(ll)p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
			max_ending_here = 0;
		if(max_ending_here>max_score_tot)
			max_score_tot = max_ending_here;
	}

	score+=max_score_tot;
	cout << score << endl;

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 700000
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=(ll)p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	int prev = 0;
	int last = 0;
	int fprev = 0;
	int flast = -1;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
		{
			max_ending_here = 0;
			prev = i+1;
			last = prev-1;
		}
		if(max_ending_here>max_score_tot)
		{
			max_score_tot = max_ending_here;
			flast = last;
			fprev = prev;
		}
		last++;
	}

	// cout << fprev << " " << flast << endl;

	int j = 0;
	while(j<fprev)
	{
		if(s[j]=='B')
			max_score_tot+=(ll)p[j];
		j++;
	}

	j = flast+1;
	while(j<n)
	{
		if(s[j]=='B')
			max_score_tot+=(ll)p[j];
		j++;
	}

	cout << max_score_tot << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 1234567
int p[N];
int diff[N];
char s[N];
int main()
{
	int n;
	cin >> n;

	for(int i = 0 ; i<n ; i++)
	{
		scanf("%d", &p[i]);
	}

	scanf("%s", s);

	ll score = 0;
	for(int i = 0 ; i<n; i++)
	{
		if(s[i]=='A')
			diff[i] = p[i];
		else
		{
			score+=p[i];
			diff[i] = -p[i];
		}
	}

	ll max_score_tot = 0;
	ll max_ending_here = 0;
	for(int i = 0 ; i<n ; i++)
	{
		max_ending_here+=(ll)diff[i];
		if(max_ending_here<0)
			max_ending_here = 0;
		if(max_ending_here>max_score_tot)
			max_score_tot = max_ending_here;
	}

	score+=max_score_tot;
	cout << score << endl;

}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll a[500010];
char ch[500010];
int main()
{
    ll x,y,z,i,t;
    cin>>x;
    for(i=0;i<x;i++)
    scanf("%d",&a[i]);
    scanf("%s",ch);
    int sum=0;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='B')
        sum+=a[i];
    }
    int hehe=sum;
    int ans=sum;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='A')
        sum+=a[i];
        else
        sum-=a[i];
        ans=max(sum,ans);
    }
    for(i=x-1;i>=0;i--)
    {
        if(ch[i]=='A')
        hehe+=a[i];
        else
        hehe-=a[i];
        ans=max(ans,hehe);
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll a[500010];
char ch[500010];
int main()
{
    ll x,y,z,i,t;
    cin>>x;
    for(i=0;i<x;i++)
    scanf("%I64d",&a[i]);
    scanf("%s",ch);
    int sum=0;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='B')
        sum+=a[i];
    }
    ll hehe=sum;
    ll ans=sum;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='A')
        sum+=a[i];
        else
        sum-=a[i];
        ans=max(sum,ans);
    }
    for(i=x-1;i>=0;i--)
    {
        if(ch[i]=='A')
        hehe+=a[i];
        else
        hehe-=a[i];
        ans=max(ans,hehe);
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll a[500010];
char ch[500010];
int main()
{
    ll x,y,z,i,t;
    cin>>x;
    for(i=0;i<x;i++)
    scanf("%I64d",&a[i]);
    scanf("%s",ch);
    ll sum=0;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='B')
        sum+=a[i];
    }
    ll hehe=sum;
    ll ans=sum;
    for(i=0;i<x;i++)
    {
        if(ch[i]=='A')
        sum+=a[i];
        else
        sum-=a[i];
        ans=max(sum,ans);
    }
    for(i=x-1;i>=0;i--)
    {
        if(ch[i]=='A')
        hehe+=a[i];
        else
        hehe-=a[i];
        ans=max(ans,hehe);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#define inf 999999999
#define mo 1000000007
#define LL long long
using namespace std;
const int maxn=600000;
long long x[maxn],s_a[maxn],s_b[maxn];
int n;
char c[maxn];
LL ans;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x[i]);
	}

	scanf("%s",c);
	for (int i=0;i<strlen(c);i++)
	{
		s_a[i+1]=s_a[i];s_b[i+1]=s_b[i];
		if (c[i]=='A') s_a[i+1]+=x[i+1];
		else s_b[i+1]+=x[i+1];
	}

	ans=s_b[n];
	for (int i=1;i<=n;i++)
		ans=max(ans,s_a[i]+s_b[n]-s_b[i]);

	for (int i=n;i>=1;i--)
		ans=max(ans,s_b[i-1]+s_a[n]-s_a[i-1]);
	cout<<ans<<endl;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#define inf 999999999
#define mo 1000000007
#define LL long long
using namespace std;
const int maxn=600000;
long long x[maxn],s_a[maxn],s_b[maxn];
int n;
char c[maxn];
LL ans;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x[i]);
	}

	scanf("%s",c);
	for (int i=0;c[i];i++)
	{
		s_a[i+1]=s_a[i];s_b[i+1]=s_b[i];
		if (c[i]=='A') s_a[i+1]+=x[i+1];
		else s_b[i+1]+=x[i+1];
	}

	int j;
	ans=s_b[n];
	for (int i=1;i<=n;i++)
	{
		ans=max(ans,s_a[i]+s_b[n]-s_b[i]);
		j=n+1-i;
		ans=max(ans,s_b[j-1]+s_a[n]-s_a[j-1]);
	}

	printf("%I64d\n",ans);
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#define inf 999999999
#define mo 1000000007
#define LL long long
using namespace std;
const int maxn=600000;
long long x[maxn],s_a[maxn],s_b[maxn];
int n;
char c[maxn];
LL ans;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x[i]);
	}

	scanf("%s",c);
	for (int i=0;c[i];i++)
	{
		s_a[i+1]=s_a[i];s_b[i+1]=s_b[i];
		if (c[i]=='A') s_a[i+1]+=x[i+1];
		else s_b[i+1]+=x[i+1];
	}

	int j;
	ans=s_b[n];
	for (int i=1;i<=n;i++)
	{
		ans=max(ans,s_a[i]+s_b[n]-s_b[i]);
		j=n+1-i;
		ans=max(ans,s_b[j-1]+s_a[n]-s_a[j-1]);
	}

	cout<<ans<<endl;
}


/**

    RAMESH CHANDRA
    NATIONAL INSTITUTE OF TECHNOLOGY KARNATKA SURATHKAL
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

   ll n;
   cin >> n;

   ll arr[n];
   for(ll i=0;i<n;i++)
        cin >> arr[i];

    string s;
    cin >> s ;

    ll price=0,checker=0,final_ans=0;

    //Calculate Current price
    for(int i=0;i<n;i++)
        if(s[i]=='B') price+=arr[i];
    checker=price;final_ans=price;

    //selecting prefix and calculate maximum ans
    for(int i=0;i<n;i++){

        if(s[i]=='A') checker+=arr[i];
        else checker-=arr[i];
        final_ans=max(final_ans,checker);
    }
    checker=price;

    //selecting suffix and calculte maximum ans
    for(int i=n-1;i>=0;i--){
        if(s[i]=='A') checker+=arr[i];
        else checker-=arr[i];
        final_ans=max(final_ans,checker);
    }

    // print final ans to judge
    cout<<final_ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

   long long int n;
   cin >> n;
   long long int arr[n];
   for(int i=0;i<n;i++)
        cin >> arr[i];
    string s;
    cin >>s ;
    long long int ans=0,profit=0,gprofit=0;
    int gsidx=0,geidx=-1,sidx=0,eidx=-1;
    for(int i=0;i<n;i++){
        if(s[i]=='A')   {profit+=arr[i]; eidx=i;}
        else profit-=arr[i];
        if(profit>gprofit){
            gprofit =profit;
            gsidx=sidx; geidx = eidx;
        }
        if(profit<0) {profit=0;sidx=i+1; }
    }
    if(profit>gprofit){
            gprofit =profit;
            gsidx=sidx; geidx = eidx;
    }
    for(int i=0;i<n;i++)
        if(s[i]=='B') ans+=arr[i];
    //cout<<gsidx<<" "<<geidx<<endl;
    if(gsidx<=geidx){
        for(int i=gsidx;i>=0 && i<=geidx && i< n;i++)
            if(s[i]=='A') ans+=arr[i];
            else ans-=arr[i];
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){

   long long int n;
   cin >> n;
   long long int arr[n];
   for(int i=0;i<n;i++)
        cin >> arr[i];
    string s;
    cin >>s ;
    long long int ans=0,profit=0;
    for(int i=0;i<n;i++){
        if(s[i]=='A')   profit+=arr[i];
        else profit-=arr[i];
        if(profit<0) profit=0;
        ans=max(profit,ans);
    }
    for(int i=0;i<n;i++)
        if(s[i]=='B') ans+=arr[i];
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){

   ll n;
   cin >> n;
   ll arr[n];
   for(ll i=0;i<n;i++)
        cin >> arr[i];
    string s;
    cin >> s ;
    ll ans=0,profit=0,gprofit=0;
    ll gsidx=0,geidx=-1,sidx=0,eidx=-1;
    for(ll i=0;i<n;i++){
        if(s[i]=='A')   {profit+=arr[i]; eidx=i;}
        else {profit-=arr[i];eidx=i;}

        if(profit>=gprofit){
            gprofit =profit;
            gsidx=sidx; geidx = eidx;
        }

        if(profit<=0) {profit=0;sidx=i+1; }
    }

    for(int i=0;i<n;i++)
        if(s[i]=='B') ans+=arr[i];
    //cout<<gsidx<<" "<<geidx<<endl;
    if(gsidx<=geidx){
        for(int i=gsidx;i>=0 && i<=geidx && i< n;i++)
            if(s[i]=='A') ans+=arr[i];
            else ans-=arr[i];
    }
    cout<<max(0LL,ans)<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,power[500007],start;
string str;
int main(){
	cin >> n;
	for(int i=0;i<n;i++)
		cin >> power[i];
	cin >> str;
	for(int i=0;i<n;i++)
		if(str[i]=='B')
			start+=power[i];
	int mini=start,asd=start;
	for(int i=n-1;i>=0;i--){
		if(str[i]=='B')
			start-=power[i];
		else
			start+=power[i];
		if(start>mini)
			mini=start;
	}
	for(int i=0;i<n;i++){
		if(str[i]=='B')
                        asd-=power[i];
                else
                        asd+=power[i];
                if(asd>mini)
                        mini=asd;
	}
	cout << mini << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,power[500007],start;
string str;
main(){
	cin >> n;
	for(int i=0;i<n;i++)
		cin >> power[i];
	cin >> str;
	for(int i=0;i<n;i++)
		if(str[i]=='B')
			start+=power[i];
	int mini=start,asd=start;
	for(int i=n-1;i>=0;i--){
		if(str[i]=='B')
			start-=power[i];
		else
			start+=power[i];
		if(start>mini)
			mini=start;
	}
	for(int i=0;i<n;i++){
		if(str[i]=='B')
                        asd-=power[i];
                else
                        asd+=power[i];
                if(asd>mini)
                        mini=asd;
	}
	cout << mini << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,power[500007],start;
string str;
int main(){
	cin >> n;
	for(int i=0;i<n;i++)
		cin >> power[i];
	cin >> str;
	for(int i=0;i<n;i++)
		if(str[i]=='B')
			start+=power[i];
	int mini=start;
	for(int i=n-1;i>=0;i--){
		if(str[i]=='B')
			start-=power[i];
		else
			start+=power[i];
		if(start>mini)
			mini=start;
	}
	cout << mini << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ForC(i, n) for (int i = 0; i < int(n); i++)
#define ForD(i, n) for (int i = int(n-1); i >= 0; i--)

using namespace std;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500500;

ll A[N], B[N];

int main (void) {
	int n;
	scanf("%d", &n);

	vector <ll> v;
	for (int i = 0; i < n; i++) {
		ll t;
		cin >> t;
		v.pb(t);
	}
	string str;
	cin >> str;

	if (str[0] == 'A') A[0] = v[0];
	else B[0] = v[0];

	for (int i = 1; i < n; i++) {
		if (str[i] == 'A') A[i] = v[i];
		else B[i] = v[i];
		A[i] += A[i-1];
		B[i] += B[i-1];
	}

	ll ans = B[n-1];
	//Prefix
	if (str[0] == 'A') {
		ans = ans + v[0];
	}
	for (int i = 1; i < n - 1; i++) {
		ll r = B[i+1];
		ll lB = B[i-1];
		ll lA = A[i-1];
		ll a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	//Inverte tudo
	ans = max (ans, A[n-1]);
	//Suffix
	if (str[n-1] == 'A') {
		ans = max (ans, B[n-1] + v[n-1]);
	}
	for (int i = n - 2; i >= 1; i--) {
		ll r = B[i+1];
		ll lB = B[i-1];
		ll lA = A[i-1];
		ll a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ForC(i, n) for (int i = 0; i < int(n); i++)
#define ForD(i, n) for (int i = int(n-1); i >= 0; i--)

using namespace std;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500500;

ll A[N], B[N];

int main (void) {
	int n;
	scanf("%d", &n);

	vector <ll> v;
	for (int i = 0; i < n; i++) {
		ll t;
		cin >> t;
		v.pb(t);
	}
	string str;
	cin >> str;

	if (str[0] == 'A') A[0] = v[0];
	else B[0] = v[0];

	for (int i = 1; i < n; i++) {
		if (str[i] == 'A') A[i] = v[i];
		else B[i] = v[i];
		A[i] += A[i-1];
		B[i] += B[i-1];
	}

	ll ans = B[n-1];
	//Prefix
	if (str[0] == 'A') {
		ans = ans + v[0];
	}
	for (int i = 1; i < n - 1; i++) {
		ll r = B[i+1] - B[i-1];
		ll lB = B[i-1];
		ll lA = A[i-1];
		ll a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	//Inverte tudo
	ans = max (ans, A[n-1]);
	//Suffix
	if (str[n-1] == 'A') {
		ans = max (ans, B[n-1] + v[n-1]);
	}
	for (int i = n - 2; i >= 1; i--) {
		ll r = B[i+1] - B[i-1];
		ll lB = B[i-1];
		ll lA = A[i-1];
		ll a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ForC(i, n) for (int i = 0; i < int(n); i++)
#define ForD(i, n) for (int i = int(n-1); i >= 0; i--)

using namespace std;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500500;

ll A[N], B[N];

int main (void) {
	int n;
	scanf("%d", &n);

	vector <ll> v;
	for (int i = 0; i < n; i++) {
		ll t;
		cin >> t;
		v.pb(t);
	}
	string str;
	cin >> str;

	if (str[0] == 'A') A[0] = v[0];
	else B[0] = v[0];

	for (int i = 1; i < n; i++) {
		if (str[i] == 'A') A[i] = v[i];
		else B[i] = v[i];
		A[i] += A[i-1];
		B[i] += B[i-1];
	}
	ll ans = B[n-1];
	//Prefix
	if (str[0] == 'A') {
		ans = ans + v[0];
	}
	for (int i = 1; i < n - 1; i++) {
		ll r = B[n-1] - B[i];
		ll lA = A[i-1];
		ll a = r + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	//Inverte tudo
	ans = max (ans, A[n-1]);
	//Suffix
	if (str[n-1] == 'A') {
		ans = max (ans, B[n-1] + v[n-1]);
	}
	for (int i = n - 2; i >= 1; i--) {
		ll lB = B[i-1];
		ll rA = A[n-1] - A[i];
		ll a = lB + rA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ForC(i, n) for (int i = 0; i < int(n); i++)
#define ForD(i, n) for (int i = int(n-1); i >= 0; i--)

using namespace std;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500500;

ll A[N], B[N];

int main (void) {
	int n;
	scanf("%d", &n);

	vector <ll> v;
	for (int i = 0; i < n; i++) {
		ll t;
		cin >> t;
		v.pb(t);
	}
	string str;
	cin >> str;

	if (str[0] == 'A') A[0] = v[0];
	else B[0] = v[0];

	for (int i = 1; i < n; i++) {
		if (str[i] == 'A') A[i] = v[i];
		else B[i] = v[i];
		A[i] += A[i-1];
		B[i] += B[i-1];
	}
	ll ans = B[n-1];
	//Prefix
	if (str[0] == 'A') {
		ans = ans + v[0];
	}
	for (int i = 1; i < n - 1; i++) {
		ll r = B[i+1] - B[i];
		ll lA = A[i-1];
		ll a = r + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	//Inverte tudo
	ans = max (ans, A[n-1]);
	//Suffix
	if (str[n-1] == 'A') {
		ans = max (ans, B[n-1] + v[n-1]);
	}
	for (int i = n - 2; i >= 1; i--) {
		ll lB = B[i-1];
		ll rA = A[i+1] - A[i];
		ll a = lB + rA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ForC(i, n) for (int i = 0; i < int(n); i++)
#define ForD(i, n) for (int i = int(n-1); i >= 0; i--)

using namespace std;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500500;

int A[N], B[N];

int main (void) {
	int n;
	scanf("%d", &n);

	vector <int> v;
	for (int i = 0; i < n; i++) {
		int t; scanf("%d", &t);
		v.pb(t);
	}
	string str;
	cin >> str;

	if (str[0] == 'A') A[0] = v[0];
	else B[0] = v[0];

	for (int i = 1; i < n; i++) {
		if (str[i] == 'A') A[i] = v[i];
		else B[i] = v[i];
		A[i] += A[i-1];
		B[i] += B[i-1];
	}

	int ans = B[n-1];
	//Prefix
	if (str[0] == 'A') {
		ans = ans + v[0];
	}
	for (int i = 1; i < n - 1; i++) {
		int r = B[i+1] - B[i-1];
		int lB = B[i-1];
		int lA = A[i-1];
		int a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	//Inverte tudo
	ans = max (ans, A[n-1]);
	//Suffix
	if (str[n-1] == 'A') {
		ans = max (ans, B[n-1] + v[n-1]);
	}
	for (int i = n - 2; i >= 1; i--) {
		int r = B[i+1] - B[i-1];
		int lB = B[i-1];
		int lA = A[i-1];
		int a = r - lB + lA;
		if (str[i] == 'A') a += v[i];
		ans = max (ans, a);
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
int main()
{
    string s;
    int n,a[100000];ll c=0;
    sf(n);
    for(int i=0;i<n;i++)sf(a[i]);
    cin >> s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='B'){c+=a[i];}
    }
    ll m = s.size()-1,t=c,t2=c;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='A'){t+=a[i];}
        else{t-=a[i];}
        if(s[m]=='A'){t2+=a[m];}
        else{t2-=a[m];}
        c = max(c,max(t,t2));
        m--;
    }
    cout << c;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
int main()
{
    string s;
    int n,a[500000];ll c=0;
    sf(n);
    for(int i=0;i<n;i++)sf(a[i]);
    cin >> s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='B'){c+=a[i];}
    }
    ll m = s.size()-1,t=c,t2=c;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='A'){t+=a[i];}
        else{t-=a[i];}
        if(s[m]=='A'){t2+=a[m];}
        else{t2-=a[m];}
        c = max(c,max(t,t2));
        m--;
    }
    cout << c;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
int main()
{
    string s;
    int n,a[100000];ll c=0;
    sf(n);
    for(int i=0;i<n;i++)sf(a[i]);
    cin >> s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='B'){c+=a[i];}
    }
    ll m = s.size()-1,t=c,t2=c;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='A'){t+=a[i];t2+=a[m];}
        else{t-=a[i];t2-=a[m];}
        c = max(c,max(t,t2));
        m--;
    }
    cout << c;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long int n;
    cin>>n;
    vector<long int> arr(n);
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    char teams[n];
    long int ans=0;
    for(int i=0;i<n;i++)
    {
        cin>>teams[i];
        if(teams[i]=='B')
        {
            ans+=arr[i];
            arr[i]*=-1;
        }
    }
    long int kadanef=0;
    long int kad=0;
    for(int c=0;c<n;c++)
    {
        kadanef=max(arr[c] + kadanef, arr[c]);
        kad=max(kadanef, kad);
    }
    ans+=kad;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long int n;
    cin>>n;
    vector<long int> arr(n);
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    char teams[n];
    long int ans=0;
    for(int i=0;i<n;i++)
    {
        cin>>teams[i];
        if(teams[i]=='B')
        {
            ans+=arr[i];
            arr[i]*=-1;
        }
    }
    long int kadanef=0;

    for(int c=0;c<n;c++)
    {
        kadanef=max(arr[c] + kadanef, arr[c]);
    }
    if(kadanef<0)
    {kadanef=0;}
    ans+=kadanef;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long int n;
    cin>>n;
    vector<long int> arr(n);
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    char teams[n];
    long int ans=0;
    for(int i=0;i<n;i++)
    {
        cin>>teams[i];
        if(teams[i]=='B')
        {
            ans+=arr[i];
            arr[i]*=-1;
        }
    }
    long int kadanef=0;
    long int kadaneb=0;
    long int kad=0;
    for(int c=0;c<n;c++)
    {
        kadanef+=arr[c];
        kadaneb+=arr[n-c-1];
        kad=max(max(kadanef, kadaneb), kad);
    }
    ans+=kad;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long int n;
    cin>>n;
    vector<long int> arr(n);
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    char teams[n];
    long long int ans=0;
    for(int i=0;i<n;i++)
    {
        cin>>teams[i];
        if(teams[i]=='B')
        {
            ans+=arr[i];
            arr[i]*=-1;
        }
    }
    long long int kadanef=0;
    long long int kadaneb=0;
    long long int kad=0;
    for(int c=0;c<n;c++)
    {
        kadanef+=arr[c];
        kadaneb+=arr[n-c-1];
        kad=max(max(kadanef, kadaneb), kad);
    }
    ans+=kad;
    cout<<ans;
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n, i;
    cin >> n;
    int *a = new int[n];
    string s;
    for (i = 0; i < n; i++)
        cin >> a[i];
    cin >> s;
    int max_pref = 0;
    int cur_pref = 0;
    for (i = 0; i < n; i++)
    {
        if (s[i] == 'A')
            cur_pref += a[i];
        else
            cur_pref -= a[i];
        if (cur_pref > max_pref)
            max_pref = cur_pref;
    }

    int max_suf = 0;
    int cur_suf = 0;
    for (i = n-1; i >= 0; i--)
    {
        if (s[i] == 'A')
            cur_suf += a[i];
        else
            cur_suf -= a[i];
        if (cur_suf > max_suf)
            max_suf = cur_suf;
    }
    int su = 0;
    for (i = 0; i < n; i++)
        if (s[i] == 'B')
            su += a[i];
    cout << su + max(max_pref, max_suf);

    delete []a;
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
    int  n, i;
    cin >> n;
    int *a = new int[n];
    string s;
    for (i = 0; i < n; i++)
        cin >> a[i];
    cin >> s;
    long long max_pref = 0;
    long long cur_pref = 0;
    for (i = 0; i < n; i++)
    {
        if (s[i] == 'A')
            cur_pref += a[i];
        else
            cur_pref -= a[i];
        if (cur_pref > max_pref)
            max_pref = cur_pref;
    }

    long long max_suf = 0;
    long long  cur_suf = 0;
    for (i = n-1; i >= 0; i--)
    {
        if (s[i] == 'A')
            cur_suf += a[i];
        else
            cur_suf -= a[i];
        if (cur_suf > max_suf)
            max_suf = cur_suf;
    }
    long long su = 0;
    for (i = 0; i < n; i++)
        if (s[i] == 'B')
            su += a[i];
    cout << su + max(max_pref, max_suf);

    delete []a;
    return 0;
}


#include <string>

using namespace std;

int main()
{
    long long  n, i;
    cin >> n;
    long long  *a = new long long [n];
    string s;
    for (i = 0; i < n; i++)
        cin >> a[i];
    cin >> s;
    long long max_pref = 0;
    long long cur_pref = 0;
    for (i = 0; i < n; i++)
    {
        if (s[i] == 'A')
            cur_pref += a[i];
        else
            cur_pref -= a[i];
        if (cur_pref > max_pref)
            max_pref = cur_pref;
    }

    long long max_suf = 0;
    long long  cur_suf = 0;
    for (i = n-1; i >= 0; i--)
    {
        if (s[i] == 'A')
            cur_suf += a[i];
        else
            cur_suf -= a[i];
        if (cur_suf > max_suf)
            max_suf = cur_suf;
    }
    long long su = 0;
    for (i = 0; i < n; i++)
        if (s[i] == 'B')
            su += a[i];
    cout << su + max(max_pref, max_suf);

    delete []a;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define len(x) (int)(x.size())
#define all(vs) vs.begin(), vs.end()
using ii = pair<int, int>;
using ll = long long;

inline int query(vector<int> vs, int l, int r){
	return vs[r]-vs[l-1];
}
int32_t main(){
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	vector<int> vs(n);
	for(auto &x:vs) cin >> x;
	string st;
	cin >> st;
	vector<int> va(n+1, 0), vb(n+1, 0);
	for(int i=0; i<n; ++i){
		if(st[i]=='A') va[i+1] = vs[i];
		else vb[i+1] = vs[i];
	}
	for(int i=1; i<=n; ++i)
		va[i] += va[i-1], vb[i]+=vb[i-1];
	int ans = query(va, 1, n);
	for(int i=1; i<=n; ++i){
		int r1 = query(vb, 1, i-1) + query(va, i, n);
		int r2 = query(va, 1, i-1) + query(vb, i, n);
		ans = max(max(r1, r2), ans);
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define len(x) (int)(x.size())
#define all(vs) vs.begin(), vs.end()
using ii = pair<int, int>;
using ll = long long;
#define MAXN 500010
ll va[MAXN], vb[MAXN];
int vs[MAXN];

ll queryA(int l, int r){
	return va[r]-va[l-1];
}
ll queryB(int l, int r){
	return vb[r]-vb[l-1];
}
int32_t main(){
	int n;
	scanf("%d", &n);
	for(int i=0; i<n; ++i) scanf("%d", &vs[i]);
	char st[n+1];
	scanf("%s", st);
	for(int i=0; i<n; ++i){
		if(st[i]=='A') va[i+1] = vs[i];
		else vb[i+1] = vs[i];
	}
	for(int i=1; i<=n; ++i)
		va[i] += va[i-1], vb[i]+=vb[i-1];

	ll ans = queryA(1, n);
	for(int i=1; i<=n; ++i){
		ll r1 = queryB(1, i-1) + queryA(i, n);
		ll r2 = queryA(1, i-1) + queryB(i, n);
		ans = max(max(r1, r2), ans);
	}
	cout << ans << endl;
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", i, a[i],Fa(i));
				}
				return 0;
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			s = 0;
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[fa[i]]) {
				for (i = 0; i < s; i++) {
					printf("%d %d\n", i, a[i]);
				}
				return 0;
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", i, a[i],Fa(i));
				}
				return 0;
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;

	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c != cnt[fa[i]]) {
				if (c + 1 == cnt[fa[i]]) {
					printf("%d\n",c);
				}
				to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
			}
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", Fa(i));
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],cnt[r[i]]);
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],Fa(r[i]));
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		if (i == a[i]) {
			printf("error!%d",i);
		}
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;

	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c != cnt[fa[i]]) {
				to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1))%mod;
			}
		}
	}
	printf("%I64d\n",(to+mod)%mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 0;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
		cnt[Fa(i)]++;

	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c != cnt[fa[i]]&& c + 1 != cnt[fa[i]]) {
				to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
			}
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	if (node == 0) {
		printf("error");
	}
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}

			to = to*(fastpow(2, cnt[Fa(i)]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],cnt[r[i]]);
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}

			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (node == 0) {
		printf("error");
	}
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],Fa(r[i]));
				}
				return 0;
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (node == 0) {
		printf("error");
	}
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", i, a[i],Fa(i));
				}
				return 0;
			}
			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 0;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
		cnt[Fa(i)]++;

	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c != cnt[fa[i]]) {
				if (c + 1 == cnt[fa[i]]) {
					printf("%d\n",c);
				}
				to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
			}
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d %d\n", r[i], a[r[i]],Fa(r[i]),cnt[i]);
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	vis[node] = 1;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;

	for (i = 1; i <= n; i++) {
		if (!vis[Fa(i)]) {
			dfs(Fa(i));
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c != cnt[fa[i]]) {
				if (c + 1 == cnt[fa[i]]) {
					printf("error!");
				}
				to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
			}
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],cnt[i]);
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
void dfs(int node) {
	if (node == 0) {
		printf("error");
	}
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			dfs(nex[node][i]);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}

			to = to*(fastpow(2, cnt[fa[i]]) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


/*/
#include "stdafx.h"
#define localtest
//*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int mod = 1e9 + 7;
int fa[201010];
int a[200010];
int vis[200010];
int cnt[200010];
int in[200010];
int r[200100];
int s = 0;
vector<int> nex[200010];
queue<int> q;
int fastpow(int a, int b) {
	int t = 1;
	while (b) {
		if (b % 2 == 1) {
			t = (__int64)t*a%mod;
		}
		a = (__int64)a*a%mod;
		b >>= 1;
	}
	return t;
}
int Fa(int n) {
	if (fa[n] != n) {
		fa[n] = Fa(fa[n]);
	}
	return fa[n];
}
int dfs(int node) {
	vis[node] = 1;
	r[s++] = node;
	if (in[node] == 1) {
		q.push(node);
	}
	int c = 1;
	for (int i = 0; i < nex[node].size(); i++) {
		if (!vis[nex[node][i]]) {
			c+=dfs(nex[node][i]);
		}
	}
	return c;
}
int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", a + i);
		fa[i] = i;
		cnt[i] = 1;
		in[i]++;
		in[a[i]]++;
		nex[i].push_back(a[i]);
		nex[a[i]].push_back(i);
	}
	for (i = 1; i <= n; i++) {
		if (Fa(i) != Fa(a[i])) {
			cnt[fa[a[i]]] += cnt[fa[i]];
			fa[fa[i]] = fa[a[i]];
		}
	}
	__int64 to = 1;
	for (i = 1; i <= n; i++) {
		if (!vis[i]) {
			s = 0;
			int t=dfs(i);
			int c = 0;
			while (!q.empty()) {
				int t = q.front();
				q.pop();
				c++;
				for (i = 0; i < nex[t].size(); i++) {
					in[nex[t][i]]--;
					if (in[nex[t][i]] == 1) {
						q.push(nex[t][i]);
					}
				}
			}
			if (c + 1 == cnt[Fa(i)]) {
				printf("%d ", cnt[Fa(i)]);
				printf("%d\n", s);
				for (i = 0; i < s; i++) {
					printf("%d %d %d\n", r[i], a[r[i]],cnt[Fa(r[i])]);
				}
				return 0;
			}
			to = to*(fastpow(2, t) - fastpow(2, c + 1)) % mod;
		}
	}
	printf("%I64d\n", (to + mod) % mod);
#ifdef localtest
	getchar();
	getchar();
#endif
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
int visited[1000000];
int cnt=0;
vector<int>vc;
int  a[1000000];
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}

void dfs2(int s,int cmp){
visited[s]=2;
cnt++;
if(visited[a[s]]==1){
dfs2(a[s],cmp);
}
else if(visited[a[s]]==2){
	if(s==cmp)
		vc.push_back(cnt);
	cnt=0;
}
visited[s]=1;
}
void dfs(int s){
	visited[s]=1;
	if(!visited[a[s]])
		dfs(a[s]);
	else{//printf("nitin%d\n",s);
			dfs2(a[s],s);

	}
}
int main(){
	int  n;
	scanf("%d",&n);
	for(ll i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		if(!visited[i])
			dfs(i);
	ll ans=1;
	int c=0;
		for(int i=0;i<vc.size();i++){
			//cout<<vc[i]<<" ";
			ll ans1=((power(2,vc[i],mod)-2)%mod+mod)%mod;
			ans=(ans1*ans)%mod;
			c+=vc[i];
		}
		c=n-c;
		ans=(ans*power(2,c,mod))%mod;
		cout<<ans<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
int visited[1000000];
int cnt=0;
vector<int>vc;
ll a[1000000];
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
void dfs2(int s,int cmp){
visited[s]=2;
cnt++;
if(visited[a[s]]==1){
dfs2(a[s],cmp);
}
else if(visited[a[s]]==2){
	if(s==cmp)
		vc.push_back(cnt);
	cnt=0;
}
visited[s]=1;
}
void dfs(int s){
	visited[s]=1;
	if(!visited[a[s]])
		dfs(a[s]);
	else{//printf("nitin%d\n",s);
			dfs2(a[s],s);

	}
}
int main(){
	ll n;
	cin>>n;
	for(ll i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		if(!visited[i])
			dfs(i);
	ll ans=1;
	int c=0;
		for(int i=0;i<vc.size();i++){
			//cout<<vc[i]<<" ";
			ll ans1=((power(2,vc[i],mod)-2)%mod+mod)%mod;
			ans=(ans1*ans)%mod;
			c+=vc[i];
		}
		c=n-c;
		ans=(ans*power(2,c,mod))%mod;
		cout<<ans<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
int visited[1000000];
int cnt=0;
vector<int>vc;
int cycle[1000000];
int  a[1000000];
bool flag=false;
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
void dfs2(int s,int cmp){
visited[s]=2;
cnt++;
if(visited[a[s]]==1){
dfs2(a[s],cmp);
}
else if(visited[a[s]]==2){
	if(s==cmp){

		vc.push_back(cnt);
		cnt=0;
		flag=true;
	}
	cnt=0;
}
visited[s]=1;
}
void dfs(int s){
	visited[s]=1;
	if(!visited[a[s]])
		dfs(a[s]);
	else{//printf("nitin%d\n",s);
	if(!cycle[a[s]]){
		flag=false;
			dfs2(a[s],s);
		}

	}
		cycle[s]=true;
}
int main(){
	int  n;
	scanf("%d",&n);
	for(ll i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		if(!visited[i])
			dfs(i);
	ll ans=1;
	int c=0;
		for(int i=0;i<vc.size();i++){
			//cout<<vc[i]<<" ";
			ll ans1=((power(2,vc[i],mod)-2)%mod+mod)%mod;
			ans=(ans1*ans)%mod;
			c+=vc[i];
		}
		c=n-c;
		ans=(ans*power(2,c,mod))%mod;
		cout<<ans<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
int visited[1000000];
int cnt=0;
vector<int>vc;
ll a[1000000];
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}

void dfs2(int s,int cmp){
visited[s]=2;
cnt++;
if(visited[a[s]]==1){
dfs2(a[s],cmp);
}
else if(visited[a[s]]==2){
	if(s==cmp)
		vc.push_back(cnt);
	cnt=0;
}
visited[s]=1;
}
void dfs(int s){
	visited[s]=1;
	if(!visited[a[s]])
		dfs(a[s]);
	else{//printf("nitin%d\n",s);
			dfs2(a[s],s);

	}
}
int main(){
	ll n;
	scanf("%l64d",&n);
	for(ll i=1;i<=n;i++)
		scanf("%l64d",&a[i]);
	for(int i=1;i<=n;i++)
		if(!visited[i])
			dfs(i);
	ll ans=1;
	int c=0;
		for(int i=0;i<vc.size();i++){
			//cout<<vc[i]<<" ";
			ll ans1=((power(2,vc[i],mod)-2)%mod+mod)%mod;
			ans=(ans1*ans)%mod;
			c+=vc[i];
		}
		c=n-c;
		ans=(ans*power(2,c,mod))%mod;
		cout<<ans<<"\n";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
int main(){
	ll n;
	cin>>n;
	ll arr[n+1];
	for(ll i=1;i<=n;i++)
		cin>>arr[i];
	ll visited[n+1];
	for(int i=0;i<=n;i++)
		visited[i]=0;
	ll curr=1;
	ll k=1;
	while(1){
		visited[curr]=k;
		k++;
		if(!visited[arr[curr]]){
			curr=arr[curr];
		}
		else{

			break;
		}
	}
	ll n1=k-visited[arr[curr]];
	ll n2=n-n1;
	ll ans1=power(2,n2,mod);
	//cout<<ans1;
	ll ans2=power(2,n1,mod);

	ans2=((ans2-2)%mod+mod)%mod;
	ans1=(ans1*ans2)%mod;
	cout<<ans1;

}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll MAXN = 200010;
const ll MOD = 1e9+7;
ll N;
ll dir[MAXN];
vector<ll> cycles;

bool vis[MAXN];
bool inCycle[MAXN];
ll dfs(ll cur){
    if (vis[dir[cur]]) return cur;
    vis[dir[cur]]=true;
    return dfs(dir[cur]);
}
ll cycleSize(ll cur, ll l, ll init){
    if (dir[cur]==init) return l+1;
    if (inCycle[cur]) return 0;
    inCycle[cur]=true;
    return cycleSize(dir[cur], l+1, init);
}
ll exp2(ll n){
    ll res = 1;
    for(ll i=0; i<n; i++){
        res = (res*2)%MOD;
    }
    return res;
}
int main(){
    // freopen("input.in", "r", stdin);
    cin>>N;
    for(ll i=0; i<N; i++){
        cin>>dir[i];
        dir[i]--;
    }
    int freeEdges = N;
    ll res = 0;
    for (int i=0; i<N; i++){
        if (vis[i]) continue;
        ll cycleStart = dfs(i);
        // cout<<cycleStart<<endl;
        ll cycleLength = cycleSize(cycleStart, 0, cycleStart);
        // cout<<i<<' '<<cycleStart<<' '<<cycleLength<<endl;
        freeEdges -= cycleLength;
        ll sgn=1;
        for(ll i=cycleLength-2; i>=0; i-=2){
            res = (res+sgn*(i+2)*exp2(i))%MOD;
            sgn*=-1;
        }
    }
    res = (res*exp2(freeEdges))%MOD;
    cout<<res<<endl;

}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll MAXN = 200010;
const ll MOD = 1e9+7;
ll N;
ll dir[MAXN];
vector<ll> cycles;

bool vis[MAXN];
ll dfs(ll cur){
    if (vis[cur]) return cur;
    vis[cur]=true;
    return dfs(dir[cur]);
}
ll cycleSize(ll cur, ll l, ll init){
    if (dir[cur]==init) return l+1;
    return cycleSize(dir[cur], l+1, init);
}
ll exp2(ll n){
    ll res = 1;
    for(ll i=0; i<n; i++){
        res = (res*2)%MOD;
    }
    return res;
}
int main(){
    // freopen("input.in", "r", stdin);
    cin>>N;
    for(ll i=0; i<N; i++){
        cin>>dir[i];
        dir[i]--;
    }
    int freeEdges = N;
    ll res = 0;
    for (int i=0; i<N; i++){
        if (vis[i]) continue;
        ll cycleStart = dfs(i);
        ll cycleLength = cycleSize(cycleStart, 0, cycleStart);
        freeEdges -= cycleLength;
        ll sgn=1;
        for(ll i=cycleLength-2; i>=0; i-=2){
            res = (res+sgn*(i+2)*exp2(i))%MOD;
            sgn*=-1;
        }
    }
    res = (res*exp2(freeEdges))%MOD;
    cout<<res<<endl;

}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll MAXN = 200010;
const ll MOD = 1e9+7;
ll N;
ll dir[MAXN];
vector<ll> cycles;

bool vis[MAXN];
bool inCycle[MAXN];
ll dfs(ll cur){
    if (vis[dir[cur]]) return cur;
    vis[dir[cur]]=true;
    return dfs(dir[cur]);
}
ll cycleSize(ll cur, ll l, ll init){
    if (dir[cur]==init) return l+1;
    if (inCycle[cur]) return 1;
    inCycle[cur]=true;
    return cycleSize(dir[cur], l+1, init);
}
ll exp2(ll n){
    ll res = 1;
    for(ll i=0; i<n; i++){
        res = (res*2)%MOD;
    }
    return res;
}
int main(){
    // freopen("input.in", "r", stdin);
    cin>>N;
    for(ll i=0; i<N; i++){
        cin>>dir[i];
        dir[i]--;
    }
    int freeEdges = N;
    ll res = 1;
    for (int i=0; i<N; i++){
        if (vis[i]) continue;
        ll cycleStart = dfs(i);
        ll cycleLength = cycleSize(cycleStart, 0, cycleStart);
        // cout<<i<<' '<<cycleStart<<' '<<cycleLength<<endl;
        if (cycleLength<=1) continue;
        freeEdges -= cycleLength;
        res = ((exp2(cycleLength)-2)*res)%MOD;
        // cout<<res<<endl;
    }
    res = (res*exp2(freeEdges))%MOD;
    cout<<res<<endl;

}


#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;
const ll MAXN = 200010;
const ll MOD = 1e9+7;
ll N;
ll dir[MAXN];

bool vis[MAXN];
ll dfs(ll cur){
    if (vis[cur]) return cur;
    vis[cur]=true;
    return dfs(dir[cur]);
}
ll cycleSize(ll cur, ll l, ll init){
    if (dir[cur]==init) return l+1;
    return cycleSize(dir[cur], l+1, init);
}
ll exp2(ll n){
    ll res = 1;
    for(ll i=0; i<n; i++){
        res = (res*2)%MOD;
    }
    return res;
}
int main(){
    // freopen("input.in", "r", stdin);
    cin>>N;
    for(ll i=0; i<N; i++){
        cin>>dir[i];
        dir[i]--;
    }
    ll cycleStart = dfs(0);
    ll cycleLength = cycleSize(cycleStart, 0, cycleStart);
    ll res = 0;
    ll sgn=1;
    for(ll i=cycleLength-2; i>=0; i-=2){
        res = (res+sgn*(i+2)*exp2(i))%MOD;
        sgn*=-1;
    }
    res = (res*exp2(N-cycleLength))%MOD;
    cout<<res<<endl;

}