#include <iostream>
#include <vector>
using namespace std;

const int LIMIT = 64;

// 入力
long long N;
long long Arr[1 << 19];
long long P[1 << 19];
long long Q[1 << 19];

// 計算
long long mod = 1000000007;
long long Answer = 0;
long long dep[1 << 19];
long long val[1 << 19];
long long inv[1 << 19];
long long val_inv[1 << 19];
long long val_inv2[1 << 19];
long long divs[1 << 19];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 32; i++) {
		if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Div(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

void init() {
	for (int i = 0; i < N; i++) {
		for (int j = (1 << i); j < (2 << i); j++) dep[j] = i;
	}
	for (int i = 0; i < (1 << N); i++) {
		long long cx = i;
		val[i] = 1;
		while (cx >= 1) {
			val[i] *= cx;
			val[i] %= mod;
			cx >>= 1;
		}
	}
	for (int i = 1; i < (1 << N); i++) inv[i] = Div(1, i, mod);
	for (int i = 1; i < (1 << N); i++) val_inv[i] = Div(1, val[i], mod);
	for (int i = 1; i < (1 << N); i++) val_inv2[i] = val_inv[i] * val_inv[i] % mod;
	for (int i = 1; i < (1 << N); i++) {
		divs[i] = inv[i];
		if (i >= 2) divs[i] *= val_inv2[i / 2];
		divs[i] %= mod;
	}
}

int getroot(int u, int v) {
	while (u != v) { u >>= 1; v >>= 1; }
	return u;
}

void dfs(int l, int r, int u, int h) {
	if (r - l == 1) return;
	int m = (l + r) / 2;

	if (r - l <= LIMIT) {
		long long ret = 0;
		for (int i = l; i < m; i++) {
			for (int j = m; j < r; j++) {
				int root = getroot((1 << (N - 1)) + Arr[i], (1 << (N - 1)) + Arr[j]);
				if (dep[root] >= h) continue;
				long long r1 = val[(1 << (N - 1)) + Arr[i]] * val[(1 << (N - 1)) + i] % mod;
				long long r2 = val[(1 << (N - 1)) + Arr[j]] * val[(1 << (N - 1)) + j] % mod;
				long long r3 = r1 * r2 % mod;
				ret += r3 * divs[root];
				ret %= mod;
			}
		}
		Answer += ret * divs[u];
		Answer %= mod;
	}
	else {
		// 木の計算
		if (h >= 1) {
			vector<long long> vec1(1 << (h + 1));
			vector<long long> vec2(1 << (h + 1));
			for (int i = l; i < r; i++) {
				int cx = (1 << (N - 1)) + Arr[i];
				while (cx >= 1) {
					if (cx < (1 << (h + 1))) {
						if (i < m) { vec1[cx] += val[(1 << (N - 1)) + Arr[i]] * val[(1 << (N - 1)) + i]; vec1[cx] %= mod; }
						else { vec2[cx] += val[(1 << (N - 1)) + Arr[i]] * val[(1 << (N - 1)) + i]; vec2[cx] %= mod; }
					}
					cx >>= 1;
				}
			}

			long long ret = 0;
			for (int i = 1; i < (1 << h); i++) {
				long long t1 = vec1[i * 2 + 0] * vec2[i * 2 + 1] % mod;
				long long t2 = vec1[i * 2 + 1] * vec2[i * 2 + 0] % mod;
				ret += (t1 + t2) * divs[i];
				ret %= mod;
			}
			Answer += ret * divs[u];
			Answer %= mod;
		}
	}

	// 再帰
	dfs(l, m, u * 2 + 0, h + 1);
	dfs(m, r, u * 2 + 1, h + 1);
}

int main() {
	// Step 1. 入力
	cin >> N;
	for (int i = 0; i < (1 << (N - 1)); i++) { cin >> P[i]; P[i]--; }
	for (int i = 0; i < (1 << (N - 1)); i++) { Q[P[i]] = i; }
	init();

	// Step 2. P における計算
	for (int i = 0; i < (1 << (N - 1)); i++) Arr[i] = P[i];
	dfs(0, (1 << (N - 1)), 1, 1);

	// Step 3. Q における計算
	for (int i = 0; i < (1 << (N - 1)); i++) Arr[i] = Q[i];
	dfs(0, (1 << (N - 1)), 1, 0);

	// Step 4. 出力
	cout << Answer << endl;
	return 0;
}

Measured memory_bytes: 19088