#include <bits/stdc++.h>
using namespace  std;
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

  
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define re(n) for (int i = 0; i < n; ++i)
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define eb emplace_back

//OutPut
#define ps(x) cout << x << endl;
#define ps_end(x) {ps(x); return;}


const int md = 1e9 + 7; //998244353 ;
const int dir[4][2] = {{-1,0}, {1, 0}, {0, -1}, {0, 1}};
template<class T> bool ckmin(T& a, const T& b) { 
  return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { 
  return a < b ? a = b, 1 : 0; }
ll pw(auto x, auto y) {  ll s = 1; while(y) { if (y & 1) s = s * x % md; x= 1LL * x * x % md; y /= 2; } return s;}

const int maxn = 1e6 + 10;

int n, m, nq, nk;
string str;
int H;
vi leaf[maxn];
int fa[maxn];
ll product[maxn];
ll inv_product[maxn];
ll sum[maxn];
vi p;
void dfs(int x, int dep) {
  
  if (dep == 1) {
    product[x] = x;
    inv_product[x] = pw(product[x], md - 2);
  } else {
    product[x] = product[fa[x]] * x % md;
    inv_product[x] = pw(product[x], md - 2);
  }
  if (dep == H) {
    leaf[x].pb(x);
    return;
  }
  fa[x * 2] = x; dfs(x * 2, dep + 1);
  fa[x * 2 + 1] = x; dfs(x * 2 + 1, dep + 1);
  for (auto y : leaf[x*2]) leaf[x].pb(y);
  for (auto y : leaf[x*2+1]) leaf[x].pb(y);
}
void task() {
  cin >> H;
  n  = 1 << (H - 1);
  p = vi(n + n);
  re(n) cin >> p[i + n], p[i+n]--, p[i+n] += 1 << (H - 1);
  debug(p);
  product[0] = 1;
  inv_product[0] = pw(product[0], md - 2);
  dfs(1, 1);
  ll ans = 0;
  re(n + n) debug(product[i]);
  re(n + n) debug(inv_product[i]);
  for (int x = 1; x < (1 << (H - 1)); ++x) {
   
    for (auto y : leaf[x*2]) {
      int z = p[y];
      while (z) {
        ll p1 = product[y] * inv_product[x / 2] % md;
        ll p2 = product[p[y]] * inv_product[z / 2] % md;
        sum[z] +=  p1 * p2 %md;
        sum[z] %= md;
        z /= 2;
      }
    } 
    for (auto y : leaf[x*2+1]) {
      int z = p[y];
      while (z/2) {
        ll p1 = product[y] * inv_product[x] % md;
        ll p2 = product[p[y]] * inv_product[z/2] % md;
        debug(y, z, sum[z^1], (z/2), p1, p2);
        ans += sum[z^1] * (z/2) % md * p1 % md * p2 %md;
        
        ans %= md;
        z /= 2;
      }
    }

    for (auto y : leaf[x*2]) {
      int z = p[y];
      while (z) {
        sum[z] = 0;
        z /= 2;  
      }
    }         
  }
  cout << ans << endl;
  //Read the stuff at the bottom
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout<<fixed<<setprecision(20);
  int T = 1;
  //cin >> T;
  for (int ti = 1; ti <= T; ++ti) {
    task();
  }
  return 0;
}

/* stuff you should look for
  * int overflow, array bounds
  * special cases (n=1?)
  * do smth instead of nothing and stay organized
  * write down
*/


Measured memory_bytes: 50584