#pragma GCC optimize("O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// gp_hash_table<int, int> mapka;

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

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.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int P = 1e9 + 7;
int mul(int a,int b){
  return a * b % P;
}
void add(int &a,int b){
  a += b;
  if(a >= P)
    a -= P;
}
vector<int> l;
vector<int> r;
vector<int> war;
vector<int> mno;

int newnode(){
  int res = SZ(l);
  l.PB(-1);
  r.PB(-1);
  war.PB(0);
  mno.PB(1);
//   roz.PB(1);
  return res;
}

void spy(int x){
  if(l[x] != -1){
    mno[l[x]] = mul(mno[l[x]],mno[x]);
    war[l[x]] = mul(war[l[x]],mno[x]);
  }
  if(r[x] != -1){
    mno[r[x]] = mul(mno[r[x]],mno[x]);
    war[r[x]] = mul(war[r[x]],mno[x]);
  }
  mno[x] = 1;
}

int out = 0;
int pomres;
int lacz(int id, int r1,int r2){
  debug("lacz",id,r1,r2);
  spy(r1);
  spy(r2);
  if(l[r1] != -1 && r[r2] != -1){
    debug("tu",l[r1],war[l[r1]], r[r2], war[r[r2]]);
    add(pomres, mul(id, mul(war[l[r1]], war[r[r2]])));
  }
  
  if(l[r2] != -1 && r[r1] != -1){
    add(pomres, mul(id, mul(war[l[r2]], war[r[r1]])));
  }
  
  int nl = -1;
  if(l[r1] != -1 && l[r2] != -1){
    nl = lacz(id*2, l[r1],l[r2]);
  }else if(l[r1] != -1){
    nl = l[r1];
  }else{
    nl = l[r2];
  }
  
  int nr = -1;
  if(r[r1] != -1 && r[r2] != -1){
    nr = lacz(id*2 + 1, r[r1], r[r2]);
  }else if(r[r1] != -1){
    nr = r[r1];
  }else{
    nr = r[r2];
  }
  l[r1] = nl;
  r[r1] = nr;
  add(war[r1], war[r2]);
  return r1;
}
int h;
int nn;
vector<int> p;
void ustwar(int id,int ak){
  war[ak] = id;
  debug(id,ak);
  if(l[ak] != -1){
    ustwar(id*2, l[ak]);
    war[ak] = mul(war[ak], war[l[ak]]);
  }
  if(r[ak] != -1){
    ustwar(id*2 + 1, r[ak]);
    war[ak] = mul(war[ak], war[r[ak]]);
  }
}
int licz(int root,int po,int ko){
  if(po + 1 == ko){
    int res = newnode();
    int ak = res;
    debug(p[po]);
    for(int i = h - 2; i>=0;i--){
      int x = newnode();
      debug(x);
      ((p[po] >> i)&1 ? r[ak] : l[ak]) = x;
      ak = x;
    }
    ustwar(1,res);
    mno[res] = root;
    debug(po,ko,war[res]);
    return res;
  }
  int a = licz(root*2, po, (po+ko)/2);
  int b = licz(root*2+1, (po+ko)/2, ko);
  pomres = 0;
  int c = lacz(1,a,b);
  debug(pomres,root);
  add(out, mul(pomres, root));
  mno[c] = mul(mno[c], root);
  return c;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> h;
  nn = (1 << (h - 1));
  p.resize(nn);
  R(i,nn){
    cin >> p[i];
    p[i]--;
  }
  debug(p);
  licz(1,0,nn);
  cout << out << "\n";
}


Measured memory_bytes: 86360