#include <bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ldb;
typedef complex<double> base;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const bool debug=false;
const ll mod=1e9+7;
ll exp(ll x,ll y){if(y<0) return 0; ll ret=1;for(;y;y>>=1,x=(x*x)%mod){if(y&1)ret=(ret*x)%mod;}return ret;}

ll H, L, ans;
ll P[25+(1<<17)];
ll DP[25+(1<<18)];
ll IV[25+(1<<18)];

struct PST
{
	int root[555555];
	int lc[11111111], rc[11111111];
	ll val[11111111], nex;
	void build(void)
	{
		root[0]=1; nex=(1<<(H));
    	for(int i=1 ; i<(1<<(H-1)) ; i++) lc[i]=(i<<1), rc[i]=(i<<1|1);
	}
	int copy(int cur)
	{
	    int x=++nex;
	    val[x]=val[cur]; lc[x]=lc[cur]; rc[x]=rc[cur];
	    return x;
	}
	int update(int cur, int s, int e, int l, int r, ll v) 
	{
	    if(l>e || r<s) return cur;
	    int x=copy(cur);
	    if(l<=s && e<=r)
	    {
	        val[x]+=v;
	      	if(val[x]>=mod) val[x]-=mod;
	        return x;
	    }
	    int m=(s+e)>>1;
	    lc[x]=update(lc[x],s,m,l,r,v);
	    rc[x]=update(rc[x],m+1,e,l,r,v);
		val[x]=val[lc[x]]+val[rc[x]];
		if(val[x]>=mod) val[x]-=mod;
   		return x;
	}
	ll eval(int cur, int loc, int s, int e, int l, int r, int HD)
	{
		ll ret=0, m=(s+e)>>1;
		if(s==HD-1 && e==HD-1) return 0;
		if(s<=HD-1 && HD-1<=m)
		{
			// m+1 ~ e is ok
			ret=val[rc[cur]];
			ret=(ret*IV[loc])%mod;
			ret=(ret*IV[loc>>1])%mod;
			ret+=eval(lc[cur], loc<<1, s, m, l, r, HD);
			if(ret>=mod) ret-=mod; return ret;
		}
		else
		{
			ret=val[lc[cur]];
			ret=(ret*IV[loc])%mod;
			ret=(ret*IV[loc>>1])%mod;
			ret+=eval(rc[cur], loc<<1|1, m+1, e, l, r, HD);
			if(ret>=mod) ret-=mod; return ret;
		}
	}
} PT;

// indexing is fucked up please

int main(void)
{
	fio; ll i, j, add; cin>>H; L=(1<<(H-1));
	for(i=1 ; i<=L ; i++) cin>>P[i];
	PT.build(); DP[0]=1; IV[0]=1; DP[1]=1; IV[1]=1;	
	for(i=2 ; i<=(1<<H) ; i++) DP[i]=(DP[i/2]*i)%mod;
	for(i=2 ; i<=(1<<H) ; i++) IV[i]=exp(DP[i], mod-2);
	ll fuck=0;
	for(i=1 ; i<=L ; i++) fuck+=(DP[L+i-1]*DP[L+P[i]-1])%mod;
	for(i=1 ; i<=L ; i++) PT.root[i]=PT.update(PT.root[i-1], 0, (1<<(H-1))-1, P[i]-1, P[i]-1, (DP[L+i-1]*DP[L+P[i]-1])%mod);
	for(i=1 ; i<=L ; i++)
	{
		ll cL=L+i-1, cR=L+i-1, p2=1, nL, nR;
		for(j=1 ; j<=H-1 ; j++)
		{
			p2<<=1; 
			if(cL%p2==0) 
			{
				nL=cL+(p2>>1);
				nR=cR+(p2>>1);
				add=PT.eval(PT.root[nR-L+1], 1, 0, (1<<(H-1))-1, 0, (1<<(H-1))-1, P[i]);
				add-=PT.eval(PT.root[nL-1-L+1], 1, 0, (1<<(H-1))-1, 0, (1<<(H-1))-1, P[i]);
				add+=mod; if(add>=mod) add-=mod;
				add=(add*DP[L+i-1])%mod; 
				add=(add*DP[L+P[i]-1])%mod;
				add=(add*IV[(L+i-1)>>j])%mod;
				add=(add*IV[(L+i-1)>>(j+1)])%mod;
				ans+=add; if(ans>=mod) ans-=mod;
				cL=min(cL, nL); cR=max(cR, nR);
			}
			else
			{
				nL=cL-(p2>>1);
				nR=cR-(p2>>1);
				add=PT.eval(PT.root[nR-L+1], 1, 0, (1<<(H-1))-1, 0, (1<<(H-1))-1, P[i]);
				add-=PT.eval(PT.root[nL-1-L+1], 1, 0, (1<<(H-1))-1, 0, (1<<(H-1))-1, P[i]);
				add+=mod; if(add>=mod) add-=mod;
				add=(add*DP[L+i-1])%mod; 
				add=(add*DP[L+P[i]-1])%mod;
				add=(add*IV[(L+i-1)>>j])%mod;
				add=(add*IV[(L+i-1)>>(j+1)])%mod;
				ans+=add; if(ans>=mod) ans-=mod;
				cL=min(cL, nL); cR=max(cR, nR);
			}
		}
	}	
	ans=(ans+100LL*mod)%mod;
	ans=(ans*exp(2, mod-2))%mod;
	cout<<ans%mod; return 0;
}


Measured memory_bytes: 47200