#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int N=1<<18,mod=1e9+7;
int h,n,p[N],tag[N],ans,stk[N],top;
inline void travel2(CI x,int prod,CI opt)
{
	if (x==1) return; prod=1LL*prod*x%mod;
	if (opt) (tag[x]+=prod)%=mod; else (ans+=1LL*(x>>1)*tag[x^1]%mod*prod%mod)%=mod;
	travel2(x>>1,prod,opt);
}
inline void travel1(CI x,int prod,CI opt)
{
	prod=1LL*prod*x%mod;
	if (x>=n) return stk[++top]=n+p[x-n]-1,travel2(n+p[x-n]-1,prod,opt);
	travel1(x<<1,prod,opt); travel1(x<<1|1,prod,opt);
}
int main()
{
	RI i; for (scanf("%d",&h),n=1<<h-1,i=0;i<n;++i)
	scanf("%d",&p[i]); for (i=1;i<n;++i)
	{
		travel1(i<<1,i,1); travel1(i<<1|1,1,0);
		while (top) for (int x=stk[top--];x;x>>=1) tag[x]=0;
	}
	return printf("%d",ans),0;
}