Inspired by the tv series Stranger Things, bear Limak is going for a walk between two mirror worlds.

There are two perfect binary trees of height H, each with the standard numeration of vertices from 1 to 2^H-1. The root is 1 and the children of x are 2 \cdot x and 2 \cdot x + 1.

Let L denote the number of leaves in a single tree, L = 2^{H-1}.

You are given a permutation P_1, P_2, \ldots, P_L of numbers 1 through L. It describes L special edges that connect leaves of the two trees. There is a special edge between vertex L+i-1 in the first tree and vertex L+P_i-1 in the second tree.

graph for sample1

drawing for the first sample test, permutation P = (2, 3, 1, 4), special edges in green

Let's define product of a cycle as the product of numbers in its vertices. Compute the sum of products of all simple cycles that have exactly two special edges, modulo (10^9+7).

A simple cycle is a cycle of length at least 3, without repeated vertices or edges.

Constraints

* 2 \leq H \leq 18
* 1 \leq P_i \leq L where L = 2^{H-1}
* P_i \neq P_j (so this is a permutation)

Input

Input is given from Standard Input in the following format (where L = 2^{H-1}).


H
P_1 P_2 \cdots P_L


Output

Compute the sum of products of simple cycles that have exactly two special edges. Print the answer modulo (10^9+7).

Examples

Input

3
2 3 1 4


Output

121788


Input

2
1 2


Output

36


Input

5
6 14 15 7 12 16 5 4 11 9 3 10 8 2 13 1


Output

10199246
#include <bits/stdc++.h>
#define BM (1<<12)
#define BT (1<<19)
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

ll memo[BM][BM];
int A[BT],B[BT];

ll score(int s,int t)
{
	if(s==t) return s+1;
	return score((s-1)/2,(t-1)/2)*(ll) (s+1)%MOD*(ll) (t+1)%MOD;
}
int main()
{
	int n;
	scanf("%d",&n);n--;
	int b=min(6,n),block=1<<b;
	//b=2,block=1<<b;
	for(int i=0;i<1<<n;i++)
	{
		scanf("%d",&A[i]);
		A[i]--;
		//A[i]=i;
		B[A[i]]=i;
		
		int a=i+(1<<n)-1,a2=A[i]+(1<<n)-1;
		vector <int> vx,vy;
		vector <ll> vs,vt;
		ll now=1;
		while(1)
		{
			now*=a+1;
			now%=MOD;
			if(a<(1<<(n-b+1))-1)
			{
				vx.push_back(a);
				vs.push_back(now);
			}
			if(a==0) break;
			a=(a-1)/2;
		}
		now=1;
		a=a2;
		while(1)
		{
			now*=a+1;
			now%=MOD;
			if(a<(1<<(n-b+1))-1)
			{
				vy.push_back(a);
				vt.push_back(now);
			}
			if(a==0) break;
			a=(a-1)/2;
		}
		for(int i=0;i<vx.size();i++)
		{
			for(int j=0;j<vy.size();j++)
			{
				memo[vx[i]][vy[j]]+=vs[i]*vt[j]%MOD;
				if(memo[vx[i]][vy[j]]>=MOD) memo[vx[i]][vy[j]]-=MOD;
			}
		}
	}/*
	for(int i=0;i<1<<n ;i++)
	{
		for(int j=0;j<1<<n;j++) printf("%lld ",memo[i][j]);
		puts("");
	}*/
	ll ret=0,ret2=0;
	for(int i=0;i<1<<n;i++)
	{
		int r=i/block;
		int r2=A[i]/block;
		for(int j=i%block+1;j<block;j++)
		{
			int v=r*block+j;
			if(A[v]/block!=r2)
			{
				ret+=score((1<<n)-1+i,(1<<n)-1+v)*score((1<<n)-1+A[i],(1<<n)-1+A[v])%MOD;
				if(ret>=MOD) ret-=MOD;
			}
		}
		for(int j=A[i]%block+1;j<block;j++)
		{
			int v=B[r2*block+j];
			ret+=score((1<<n)-1+i,(1<<n)-1+v)*score((1<<n)-1+A[i],(1<<n)-1+A[v])%MOD;
			if(ret>=MOD) ret-=MOD;
		}
		
		if(b<n)
		{
			int a=i+(1<<n)-1,a2=A[i]+(1<<n)-1;
			vector <int> vx,vy;
			vector <ll> vs,vt;
			ll now=1;
			int bef=-1;
			while(1)
			{
				now*=a+1;
				now%=MOD;
				if(a<(1<<(n-b))-1)
				{
					int to=2*a+1;
					if(bef==to) to++;
					vx.push_back(to);
					vs.push_back(now);
				}
				if(a==0) break;
				bef=a;
				a=(a-1)/2;
			}
			now=1;
			bef=-1;
			a=a2;
			while(1)
			{
				now*=a+1;
				now%=MOD;
				if(a<(1<<(n-b))-1)
				{
					int to=2*a+1;
					if(bef==to) to++;
					vy.push_back(to);
					vt.push_back(now);
				}
				if(a==0) break;
				bef=a;
				a=(a-1)/2;
			}
			for(int j=0;j<vx.size();j++)
			{
				for(int k=0;k<vy.size();k++)
				{
					//if(memo[vx[j]][vy[k]]!=0) printf("%d : %d %d\n",i,vx[j],vy[k]);
					ret2+=vs[j]*vt[k]%MOD*memo[vx[j]][vy[k]]%MOD;
					if(ret2>=MOD) ret2-=MOD;
				}
			}
		}
	}
	if(ret2%2==1) ret2+=MOD;
	ret2/=2;	
	printf("%lld\n",(ret+ret2)%MOD);
}