#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define FORN(i, x, y) for (int i = x; i <= y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define scan1(X) scanf("%d", &X)
#define scan2(X, Y) scanf("%d%d", &X, &Y)
#define scan3(X, Y, Z) scanf("%d%d%d", &X, &Y, &Z)
#define scan4(X, Y, Z, W) scanf("%d%d%d%d", &X, &Y, &Z, &W)
#define scanl1(X) scanf("%lld", &X);
#define scanl2(X, Y) scanf("%lld%lld", &X, &Y)
#define scanl3(X, Y, Z) scanf("%lld%lld%lld", &X, &Y, &Z)
#define scanl4(X, Y, Z, W) scanf("%lld%lld%lld%lld", &X, &Y, &Z, &W)
#define print1(X) printf("%d\n", X);
#define print2(X, Y) printf("%d %d\n", X, Y);
#define print3(X, Y, Z) printf("%d %d %d\n", X, Y, Z);
#define print4(X, Y, Z, W) printf("%d %d %d %d\n", X, Y, Z, W);
#define printl1(X) printf("%lld\n", X);
#define printl2(X, Y) printf("%lld %lld\n", X, Y);
#define printl3(X, Y, Z) printf("%lld %lld %lld\n", X, Y, Z);
#define printl4(X, Y, Z, W) printf("%lld %lld %lld %lld\n", X, Y, Z, W);
#define max3(X, Y, Z) max(X, max(Y, Z))
#define max4(X, Y, Z, W) max(max(X, Y), max(Z, W))
#define min3(X, Y, Z) min(X, min(Y, Z))
#define min4(X, Y, Z, W) min(min(X, Y), min(Z, W))
#define popcount(X) __builtin_popcount(X)
#define all(x) x.begin(), x.end()
#define size(X) (int)X.size()
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define lc(x) 2 * x
#define rc(x) 2 * x + 1
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pldld;
const long long l_inf = numeric_limits<long long>::max();
const long long l_ninf = numeric_limits<long long>::min();
const int inf = numeric_limits<int>::max();
const int ninf = numeric_limits<int>::min();
const double pi = 1.00 * acos(-1.00);
const double eps = 1e-8;
void cio(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }
int sq( int x ) {return x * x;}
////////////////////////////////////////////////////////////////////Header
const int maxn = 1<<18;
const int MOD = 1e9 + 7;
int mult ( int a, int b ) { return (1ll * a * b)%MOD; }
int add ( int a, int b ) { return ( a + b ) % MOD; } 
int h, n;
int p[maxn], sum[maxn];
vector<int> node_clear;
int ans;

void trav2 ( int id, int prod, bool left ) {
    if ( id == 1 ) return;

    prod = mult(id, prod);

    if ( left ) sum[id] = add(sum[id], prod);
    else ans = add(ans, mult(id/2, mult(prod, sum[id^1])));

    trav2(id/2, prod, left);
}

void trav1 ( int id, int prod, bool left ) {
    prod = mult(id, prod);
    if ( id < n ) {
        trav1(lc(id), prod, left);
        trav1(rc(id), prod, left);
    }
    else { //meet leaf node
        int nxt = p[id - n + 1] + n - 1;
        trav2(nxt, prod, left);
        node_clear.pb(nxt);
    }
}

void solve () {
    FORN(i, 1, n - 1) {
        trav1(lc(i), i, true);
        trav1(rc(i), 1, false);
        for ( auto ni : node_clear ) {
            while ( ni >= 1 ) {
                sum[ni] = 0;
                ni >>= 1;
            }
        }
        node_clear.clear();
    }
    print1(ans);
}

int main () {
    scan1(h);
    FORN(i, 1, 1<<(h - 1)) scan1(p[i]);
    n = 1<<(h - 1);
    solve();
}


Measured memory_bytes: 5484