#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
	int val;
	Mint() { val = 0; }
	Mint(const ll& x) {
		val = (-MOD <= x && x < MOD) ? x : x % MOD;
		if (val < 0) val += MOD;
	}
	template <typename U>
	explicit operator U() const { return (U)val; }
	friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
	friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
	friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
	Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
	friend Mint modex(Mint a, ll p) {
		assert(p >= 0);
		Mint ans = 1;
		for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
	Mint& operator++() { return *this += 1; }
	Mint& operator--() { return *this -= 1; }
	Mint operator++(int) { Mint result(*this); *this += 1; return result; }
	Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
	Mint operator-() const { return Mint(-val); }
	friend Mint operator+(Mint a, const Mint& b) { return a += b; }
	friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
	friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
	friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
	friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
};
int main() {
	int H;
	scanf("%d", &H);
	int L = 1 << (H - 1);
	vector<int> P(L + 1);
	for (int i = 1; i <= L; ++i) scanf("%d", &P[i]);
	Mint ans = 0;
	vector<vector<Mint>> cnt(L, vector<Mint>(2));
	for (int i = 1; i < L; ++i) {
		int st, w = 1;
		for (st = i; st < L; st *= 2, w *= 2);
		for (int j = st; j < st + w / 2; ++j) {
			int k = L - 1 + P[j - L + 1], pp;
			Mint prod = 1;
			for (int p = j; p >= i; p >>= 1) prod *= p;
			prod *= k;
			for (int p = k / 2, pp = k; p >= 1; pp = p, p >>= 1) {
				prod *= p;
				cnt[p][pp & 1] += prod;
			}
		}
		for (int j = st + w / 2; j < st + w; ++j) {
			int k = L - 1 + P[j - L + 1], pp;
			Mint prod = 1;
			for (int p = j; p > i; p >>= 1) prod *= p;
			prod *= k;
			for (int p = k / 2, pp = k; p >= 1; pp = p, p >>= 1) {
				ans += prod * cnt[p][!(pp & 1)];
				prod *= p;
			}
		}
		for (int j = st; j < st + w / 2; ++j) {
			int k = L - 1 + P[j - L + 1], pp;
			for (int p = k / 2, pp = k; p >= 1; pp = p, p >>= 1) {
				cnt[p][pp & 1] = 0;
			}
		}
	}
	printf("%d\n", ans.val);
}


Measured memory_bytes: 10916