//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353

ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}


ll sum[(1<<18)];
vector<int>node[(1<<18)];
int h, a[(1<<18)];
ll mul[(1<<18)], rev[(1<<18)], revn[(1<<18)], ans, coef[(1<<18)], MM[(1<<18)];
int L;
void make(int v, int num){
	if(v == 1) return;
	int up = v>>1;
	if((up<<1) == v){
		node[(up<<1)+1].pb(num);
	}
	else{
		node[(up<<1)].pb(num);
	}
	make(up, num);
}
void add(ll val, int pos){
	sum[pos] += val;
	while(sum[pos] >= mod) sum[pos] -= mod;
	if(pos == 1) return;
	add(val, (pos >> 1));
}
ll get_sum(int pos){
	if(pos == 1) return 0;
	int up = (pos >> 1);
	if((up<<1) == pos){
		return sum[(up<<1)+1] * coef[up] % mod /*rev[(up>>1)] % mod * revn[up] % mod*/ + get_sum(up);
	}
	else{
		return sum[(up<<1)] * coef[up] % mod /*rev[(up>>1)] % mod * revn[up] % mod*/ + get_sum(up);
	}
}
void dfs(int id, int l, int r){
	for(int x=l;x<=r;x++){
		add(MM[x]/*mul[a[x-L]] * mul[x] % mod*/, a[x-L]);
	}
	for(int i=0;i<node[id].size();i++){
		assert(id != 1);
		ll S = get_sum(a[node[id][i]-L]) % mod;
		ans += S * coef[(id>>1)] /*rev[(id>>2)] % mod * revn[(id>>1)] */ % mod * MM[node[id][i]]/*mul[node[id][i]] % mod * mul[a[node[id][i]-L]] */% mod;
	}
	for(int x=l;x<=r;x++){
		add(mod-MM[x]/*mul[a[x-L]] * mul[x] % mod*/, a[x-L]);
	}
	if(id*2 < 2*L) dfs(id*2, l, (l+r)/2);
	if(id*2+1 < 2*L) dfs(id*2+1, (l+r)/2+1, r);
}
int main(){
	scanf("%d", &h);
	L = (1<<(h-1));
	for(int i=0;i<L;i++){
		scanf("%d", &a[i]);
		a[i] += L-1;
		make(i + L, i + L);
	}
	mul[0] = 1;
	for(int i=1;i<2*L;i++){
		mul[i] = mul[i>>1] * (ll)(i) % mod;
	}
	rep(i, 2*L) rev[i] = modpow(mul[i], mod-2) * modpow(mul[i], mod-2) % mod;
	rep(i, 2*L) revn[i] = modpow((ll)(i), mod-2);
	
	rep(i, 2*L) coef[i] = rev[(i>>1)] * revn[i] % mod;
	for(int i=L;i<2*L;i++) MM[i] = mul[a[i-L]] * mul[i] % mod;
	dfs(1, L, 2*L-1);
	ans = (ans%mod+mod)%mod;
  	ans = ans * modpow(2LL, mod-2) % mod;
	printf("%lld\n", ans);
}


Measured memory_bytes: 36256