#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rz resize
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N=(1<<19)+5;
const int mod=1e9+7;
int h,m,a[N],f[N],sz[N],vs[2][N];
set<int>s[N];
vector<int>ps[N],ps0[N];
vector<int>ord[N],ord0[N],ord1[N];
vector<int>bit[N];
void cl(int x,int rt){
b[x]=a[x];
ps[rt].pb(x);
ps0[rt].pb(a[x]);
if(x*2<(1<<h))cl(x*2,rt),cl(x*2+1,rt);
}
void work(int x){
if(x*2>=(1<<h)||!b[x*2]&&!b[x*2+1]){
b[x]=0;
return;
}
if(b[x*2]>b[x*2+1]){
b[x]=b[x*2];
work(x*2);
}
else{
b[x]=b[x*2+1];
work(x*2+1);
}
}
int get(int x){
if(x*2>=(1<<h)||!b[x*2]&&!b[x*2+1])return x;
if(b[x*2]>b[x*2+1])return get(x*2);
return get(x*2+1);
}
int id(int i,int x){
return lower_bound(ps[i].begin(),ps[i].end(),x)-ps[i].begin();
}
int id0(int i,int x){
return lower_bound(ps0[i].begin(),ps0[i].end(),x)-ps0[i].begin();
}
void add(int x,int y,int z){
for(;y<=sz[x];y+=y&-y)bit[x][y]+=z;
}
int ask(int x,int y){
int z=0;
for(;y;y-=y&-y)z+=bit[x][y];
return z;
}
int main(){
//freopen("data.in","r",stdin);
//freopen("swap.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>h;for(int i=1;i<1<<h;i++)cin>>a[i];
cin>>m;f[0]=1;for(int i=1;i<1<<h;i++)f[i]=f[i-1]*2%mod;
for(int i=1;i<1<<j;i++){
cl(i,i);
sort(ps[i].begin(),ps[i].end());
sort(ps0[i].begin(),ps0[i].end());
sz[i]=ps[i].size();
ord[i].rz(sz[i]);
ord0[i].rz(sz[i]);
bit[i].rz(sz[i]+1);
int tmp=0;
while(a[i]){
int x=get(i);
ord[i][id0(i,a[i])]=tmp;
ord1[i][id0(i,a[i])]=x;
tmp++;
ord[i][id(i,x)]=tmp;
work(i);
}
}
for(int i=1;i<=m;i++){
int op,x,y;
cin>>op>>x>>y;
if(op==1){
vs[y-1][x]^=1;
if(y==1){
for(int j=x;j;j/=2){
s[j].insert(ord[j][id(j,x)]);
}
}
else{
for(int j=x;j;j/=2){
add(j,ord[j][id(j,x)],1);
}
}
}
else if(op==2){
vs[y-1][x]^=1;
if(y==1){
for(int j=x;j;j/=2){
s[j].erase(ord[j][id(j,x)]);
}
}
else{
for(int j=x;j;j/=2){
add(j,ord[j][id(j,x)],-1);
}
}
}
else{
int p=get0(x,y);
if(p==sz[x]||ps0[x][p]!=y)cout<<0<<"\n";
else{
int t=ord0[x][p],z=ord1[x][p];
if(s[x].size()&&*s[x].begin()<=t||!vs[0][z]&&!vs[1][z]){
cout<<0<<"\n";
}
else{
int cnt=ask(x,sz[x])-ask(x,t);
if(vs[1][z])cnt--;
cout<<f[cnt]<<"\n";
}
}
}
}
}