#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-9;
const long double pi = acos(-1.0);
long long mod_pow(long long a, long long n, long long m) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
struct modint {
  long long n;
  constexpr modint() : n(0) { ; }
  constexpr modint(const long long m) : n(m) {
    if (n >= mod)
      n %= mod;
    else if (n < 0)
      n = (n % mod + mod) % mod;
  }
  operator int() { return n; }
};
constexpr bool operator==(const modint a, const modint b) noexcept {
  return a.n == b.n;
}
constexpr modint operator+=(modint &a, const modint b) noexcept {
  a.n += b.n;
  if (a.n >= mod) a.n -= mod;
  return a;
}
constexpr modint operator-=(modint &a, const modint b) noexcept {
  a.n -= b.n;
  if (a.n < 0) a.n += mod;
  return a;
}
constexpr modint operator*=(modint &a, const modint b) noexcept {
  a.n = ((long long)a.n * b.n) % mod;
  return a;
}
constexpr modint operator+(modint a, const modint b) noexcept { return a += b; }
constexpr modint operator-(modint a, const modint b) noexcept { return a -= b; }
constexpr modint operator*(modint a, const modint b) noexcept { return a *= b; }
constexpr modint operator^(const modint a, const long long n) noexcept {
  if (n == 0) return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2) res = res * a;
  return res;
}
long long inv(long long a, long long p) {
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
constexpr modint operator/(const modint a, const modint b) noexcept {
  return a * (b ^ (mod - 2));
}
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++) {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--) {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b) {
  if (a < 0 || b < 0 || a < b) return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;
string sdir = "RDLU";
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
modint t2[1 << 19];
modint invt2[1 << 19];
modint inv2;
void init(int n) {
  t2[0] = 1;
  invt2[0] = 1;
  inv2 = (modint)1 / (modint)2;
  for (int i = 0; i < n + 5; i++) {
    t2[i + 1] = t2[i] * (modint)2;
    invt2[i + 1] = invt2[i] * inv2;
  }
}
struct SegT {
 private:
  int n;
  vector<modint> node, lazy;
  const modint init_c = 0;

 public:
  SegT(int sz) {
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, init_c);
    lazy.resize(2 * n - 1, 1);
  }
  SegT(vector<modint> &v) {
    int sz = v.size();
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, init_c);
    lazy.resize(2 * n - 1, 1);
    for (int i = 0; i < sz; i++) {
      node[i + n - 1] = v[i];
    }
    for (int i = n - 1 - 1; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  modint f(modint a, modint b) { return a + b; }
  void eval(int k, int l, int r) {
    if (lazy[k] == (modint)1) return;
    node[k] *= lazy[k];
    if (r - l > 1) {
      lazy[2 * k + 1] *= lazy[k];
      lazy[2 * k + 2] *= lazy[k];
    }
    lazy[k] = 1;
  }
  void add(modint x, int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      lazy[k] *= x;
      eval(k, l, r);
    } else {
      add(x, a, b, k * 2 + 1, l, (l + r) / 2);
      add(x, a, b, k * 2 + 2, (l + r) / 2, r);
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  modint query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return init_c;
    if (a <= l && r <= b)
      return node[k];
    else {
      modint vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      modint vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
  void update(int c, modint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= c || c + 1 <= l) return;
    if (c <= l && r <= c + 1) {
      node[k] = x;
    } else {
      update(c, x, k * 2 + 1, l, (l + r) / 2);
      update(c, x, k * 2 + 2, (l + r) / 2, r);
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }
};
struct BIT {
 private:
  vector<long long> node;
  int n;

 public:
  BIT(int n_) {
    n = n_;
    node.resize(n, 0);
  }
  void add(int a, long long w) {
    for (int i = a; i < n; i |= i + 1) node[i] += w;
  }
  long long sum(int a) {
    long long ret = 0;
    for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1) ret += node[i];
    return ret;
  }
  long long sum(int a, int b) { return sum(b) - sum(a); }
};
void solve() {
  int n;
  cin >> n;
  init(n);
  vector<pair<long long, long long> > vs;
  vector<long long> in(n);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    vs.push_back({in[i], i});
  }
  int q;
  cin >> q;
  vector<int> locs(q);
  vector<long long> vals(q);
  for (int i = 0; i < q; i++) {
    cin >> locs[i];
    locs[i]--;
    cin >> vals[i];
    vs.push_back({vals[i], i + n});
  }
  sort((vs).begin(), (vs).end());
  BIT memo(n + q);
  modint sum = 0;
  vector<modint> ori1(n + q, 0), ori2(n + q, 0);
  int tmp = 0;
  modint csum = 0;
  for (int i = 0; i < n + q; i++) {
    int id = vs[i].second;
    if (id < n) {
      modint ad = csum * invt2[tmp] * inv2 * (modint)in[id];
      sum += ad;
      ori1[i] = t2[tmp] * (modint)in[id];
      ori2[i] = invt2[tmp] * (modint)in[id];
      memo.add(i, 1);
      tmp++;
      csum += ori1[i];
    }
  }
  SegT st1(ori1);
  SegT st2(ori2);
  vector<int> cur(n);
  vector<int> tloc(q);
  for (int i = 0; i < n + q; i++) {
    int id = vs[i].second;
    if (id < n) {
      cur[id] = i;
    } else {
      tloc[id - n] = i;
    }
  }
  cout << sum << endl;
  for (int i = 0; i < q; i++) {
    int loc = cur[locs[i]];
    modint val = in[locs[i]];
    modint dif = sum;
    {
      int cl = memo.sum(0, loc);
      modint lesum = st1.query(0, loc);
      modint risum = st2.query(loc + 1, n + q);
      sum += lesum * risum * inv2;
      lesum = lesum * invt2[cl] * inv2 * val;
      sum -= lesum;
      risum = risum * t2[cl] * inv2 * val;
      sum -= risum;
      memo.add(loc, -1);
      st1.update(loc, 0);
      st1.add(inv2, loc + 1, n + q);
      st2.update(loc, 0);
      st2.add(2, loc + 1, n + q);
    }
    dif = dif - sum;
    loc = tloc[i];
    val = vals[i];
    {
      int cl = memo.sum(0, loc);
      memo.add(loc, 1);
      st1.update(loc, val * t2[cl]);
      st1.add(2, loc + 1, n + q);
      st2.update(loc, val * invt2[cl]);
      st2.add(inv2, loc + 1, n + q);
      modint lesum = st1.query(0, loc);
      modint risum = st2.query(loc + 1, n + q);
      sum -= lesum * risum * inv2;
      lesum = lesum * invt2[cl] * inv2 * val;
      sum += lesum;
      risum = risum * t2[cl] * inv2 * val;
      sum += risum;
    }
    in[locs[i]] = vals[i];
    cur[locs[i]] = loc;
    cout << sum << "\n";
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  char nyaa;
  cin >> nyaa;
  return 0;
}

### Prompt

Please formulate a CPP solution to the following problem:

There are n officers in the Army of Byteland. Each officer has some power associated with him. The power of the i-th officer is denoted by p_{i}. As the war is fast approaching, the General would like to know the strength of the army.

The strength of an army is calculated in a strange way in Byteland. The General selects a random subset of officers from these n officers and calls this subset a battalion.(All 2^n subsets of the n officers can be chosen equally likely, including empty subset and the subset of all officers).

The strength of a battalion is calculated in the following way:

Let the powers of the chosen officers be a_{1},a_{2},…,a_{k}, where a_1 ≤ a_2 ≤ ... ≤ a_k. The strength of this battalion is equal to a_1a_2 + a_2a_3 + ... + a_{k-1}a_k. (If the size of Battalion is ≤ 1, then the strength of this battalion is 0).

The strength of the army is equal to the expected value of the strength of the battalion.

As the war is really long, the powers of officers may change. Precisely, there will be q changes. Each one of the form i x indicating that p_{i} is changed to x.

You need to find the strength of the army initially and after each of these q updates.

Note that the changes are permanent.

The strength should be found by modulo 10^{9}+7. Formally, let M=10^{9}+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and qnot≡ 0 mod M). Output the integer equal to p⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p mod M).

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 3⋅10^{5}) — the number of officers in Byteland's Army.

The second line contains n integers p_{1},p_{2},…,p_{n} (1 ≤ p_{i} ≤ 10^{9}).

The third line contains a single integer q (1 ≤ q ≤ 3⋅10^{5}) — the number of updates.

Each of the next q lines contains two integers i and x (1 ≤ i ≤ n,  1 ≤ x ≤ 10^{9}), indicating that p_{i} is updated to x .

Output

In the first line output the initial strength of the army.

In i-th of the next q lines, output the strength of the army after i-th update.

Examples

Input


2
1 2
2
1 2
2 1


Output


500000004
1
500000004


Input


4
1 2 3 4
4
1 5
2 5
3 5
4 5


Output


625000011
13
62500020
375000027
62500027

Note

In first testcase, initially, there are four possible battalions 

  * {} Strength = 0 
  * {1} Strength = 0 
  * {2} Strength = 0 
  * {1,2} Strength = 2 

So strength of army is (0+0+0+2)/(4) = 1/2

After changing p_{1} to 2, strength of battallion {1,2} changes to 4, so strength of army becomes 1.

After changing p_{2} to 1, strength of battalion {1,2} again becomes 2, so strength of army becomes 1/2.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-9;
const long double pi = acos(-1.0);
long long mod_pow(long long a, long long n, long long m) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
struct modint {
  long long n;
  constexpr modint() : n(0) { ; }
  constexpr modint(const long long m) : n(m) {
    if (n >= mod)
      n %= mod;
    else if (n < 0)
      n = (n % mod + mod) % mod;
  }
  operator int() { return n; }
};
constexpr bool operator==(const modint a, const modint b) noexcept {
  return a.n == b.n;
}
constexpr modint operator+=(modint &a, const modint b) noexcept {
  a.n += b.n;
  if (a.n >= mod) a.n -= mod;
  return a;
}
constexpr modint operator-=(modint &a, const modint b) noexcept {
  a.n -= b.n;
  if (a.n < 0) a.n += mod;
  return a;
}
constexpr modint operator*=(modint &a, const modint b) noexcept {
  a.n = ((long long)a.n * b.n) % mod;
  return a;
}
constexpr modint operator+(modint a, const modint b) noexcept { return a += b; }
constexpr modint operator-(modint a, const modint b) noexcept { return a -= b; }
constexpr modint operator*(modint a, const modint b) noexcept { return a *= b; }
constexpr modint operator^(const modint a, const long long n) noexcept {
  if (n == 0) return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2) res = res * a;
  return res;
}
long long inv(long long a, long long p) {
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
constexpr modint operator/(const modint a, const modint b) noexcept {
  return a * (b ^ (mod - 2));
}
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++) {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--) {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b) {
  if (a < 0 || b < 0 || a < b) return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;
string sdir = "RDLU";
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
modint t2[1 << 19];
modint invt2[1 << 19];
modint inv2;
void init(int n) {
  t2[0] = 1;
  invt2[0] = 1;
  inv2 = (modint)1 / (modint)2;
  for (int i = 0; i < n + 5; i++) {
    t2[i + 1] = t2[i] * (modint)2;
    invt2[i + 1] = invt2[i] * inv2;
  }
}
struct SegT {
 private:
  int n;
  vector<modint> node, lazy;
  const modint init_c = 0;

 public:
  SegT(int sz) {
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, init_c);
    lazy.resize(2 * n - 1, 1);
  }
  SegT(vector<modint> &v) {
    int sz = v.size();
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, init_c);
    lazy.resize(2 * n - 1, 1);
    for (int i = 0; i < sz; i++) {
      node[i + n - 1] = v[i];
    }
    for (int i = n - 1 - 1; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  modint f(modint a, modint b) { return a + b; }
  void eval(int k, int l, int r) {
    if (lazy[k] == (modint)1) return;
    node[k] *= lazy[k];
    if (r - l > 1) {
      lazy[2 * k + 1] *= lazy[k];
      lazy[2 * k + 2] *= lazy[k];
    }
    lazy[k] = 1;
  }
  void add(modint x, int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      lazy[k] *= x;
      eval(k, l, r);
    } else {
      add(x, a, b, k * 2 + 1, l, (l + r) / 2);
      add(x, a, b, k * 2 + 2, (l + r) / 2, r);
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  modint query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= a || b <= l) return init_c;
    if (a <= l && r <= b)
      return node[k];
    else {
      modint vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      modint vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
  void update(int c, modint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (r <= c || c + 1 <= l) return;
    if (c <= l && r <= c + 1) {
      node[k] = x;
    } else {
      update(c, x, k * 2 + 1, l, (l + r) / 2);
      update(c, x, k * 2 + 2, (l + r) / 2, r);
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }
};
struct BIT {
 private:
  vector<long long> node;
  int n;

 public:
  BIT(int n_) {
    n = n_;
    node.resize(n, 0);
  }
  void add(int a, long long w) {
    for (int i = a; i < n; i |= i + 1) node[i] += w;
  }
  long long sum(int a) {
    long long ret = 0;
    for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1) ret += node[i];
    return ret;
  }
  long long sum(int a, int b) { return sum(b) - sum(a); }
};
void solve() {
  int n;
  cin >> n;
  init(n);
  vector<pair<long long, long long> > vs;
  vector<long long> in(n);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    vs.push_back({in[i], i});
  }
  int q;
  cin >> q;
  vector<int> locs(q);
  vector<long long> vals(q);
  for (int i = 0; i < q; i++) {
    cin >> locs[i];
    locs[i]--;
    cin >> vals[i];
    vs.push_back({vals[i], i + n});
  }
  sort((vs).begin(), (vs).end());
  BIT memo(n + q);
  modint sum = 0;
  vector<modint> ori1(n + q, 0), ori2(n + q, 0);
  int tmp = 0;
  modint csum = 0;
  for (int i = 0; i < n + q; i++) {
    int id = vs[i].second;
    if (id < n) {
      modint ad = csum * invt2[tmp] * inv2 * (modint)in[id];
      sum += ad;
      ori1[i] = t2[tmp] * (modint)in[id];
      ori2[i] = invt2[tmp] * (modint)in[id];
      memo.add(i, 1);
      tmp++;
      csum += ori1[i];
    }
  }
  SegT st1(ori1);
  SegT st2(ori2);
  vector<int> cur(n);
  vector<int> tloc(q);
  for (int i = 0; i < n + q; i++) {
    int id = vs[i].second;
    if (id < n) {
      cur[id] = i;
    } else {
      tloc[id - n] = i;
    }
  }
  cout << sum << endl;
  for (int i = 0; i < q; i++) {
    int loc = cur[locs[i]];
    modint val = in[locs[i]];
    modint dif = sum;
    {
      int cl = memo.sum(0, loc);
      modint lesum = st1.query(0, loc);
      modint risum = st2.query(loc + 1, n + q);
      sum += lesum * risum * inv2;
      lesum = lesum * invt2[cl] * inv2 * val;
      sum -= lesum;
      risum = risum * t2[cl] * inv2 * val;
      sum -= risum;
      memo.add(loc, -1);
      st1.update(loc, 0);
      st1.add(inv2, loc + 1, n + q);
      st2.update(loc, 0);
      st2.add(2, loc + 1, n + q);
    }
    dif = dif - sum;
    loc = tloc[i];
    val = vals[i];
    {
      int cl = memo.sum(0, loc);
      memo.add(loc, 1);
      st1.update(loc, val * t2[cl]);
      st1.add(2, loc + 1, n + q);
      st2.update(loc, val * invt2[cl]);
      st2.add(inv2, loc + 1, n + q);
      modint lesum = st1.query(0, loc);
      modint risum = st2.query(loc + 1, n + q);
      sum -= lesum * risum * inv2;
      lesum = lesum * invt2[cl] * inv2 * val;
      sum += lesum;
      risum = risum * t2[cl] * inv2 * val;
      sum += risum;
    }
    in[locs[i]] = vals[i];
    cur[locs[i]] = loc;
    cout << sum << "\n";
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  char nyaa;
  cin >> nyaa;
  return 0;
}

```