#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)

int const N = 1 << 18, md = 1e9 + 7;
int p[N], fr[N], H, L, an;

void ad(int &x, int y) { if ((x += y) >= md)x -= md; }

vector<pair<int, int> > go(int v = 1, int h = 1){
	if (h == H)return vector<pair<int, int> > { { v, v } };
	auto a = go(v << 1, h + 1), b = go(v << 1 | 1, h + 1);
	
	f(i, 0, a.size()){
		int u = p[a[i].first - L + 1] + L - 1, c = a[i].second;
		while (u > 1){
			c = (ll)c * u % md;
			ad(fr[u], c);
			u >>= 1;
		}
	}
	
	f(i, 0, b.size()){
		int u = p[b[i].first - L + 1] + L - 1, c = b[i].second;
		while (u > 1){
			c = (ll)c * u % md;
			ad(an, (ll)fr[u ^ 1] * c % md * (u >> 1) % md * v % md);
			u >>= 1;
		}
	}
	
	f(i, 0, a.size()){
		int u = p[a[i].first - L + 1] + L - 1;
		while (u > 1){
			fr[u] = 0;
			u >>= 1;
		}
	}
	a.insert(a.end(), b.begin(), b.end());
	b.clear();
	for (auto &t: a)t.second = (ll)t.second * v % md;
	return a;
}

int main(){
	scanf("%d", &H);
	L = 1 << H - 1;
	f(i, 1, L + 1)scanf("%d", p + i);
	go();
	printf("%d\n", an);
}