#include <bits/stdc++.h>
using namespace std;
int n, m;
int sum[500005], po[500005], ny[500005], fa[500005], flag[500005], an[500005],
    la[500005], sa[500005];
long long s[500005];
struct qus {
  int l, r, id, ans, t1, t2;
} q[500005];
bool cmp(const qus &A, const qus &B) { return A.r < B.r; }
int poi(int a, int b) {
  int an = 1;
  while (b != 0) {
    if (b & 1) an = (long long)an * a % 1000000007;
    a = (long long)a * a % 1000000007;
    b = (b >> 1);
  }
  return an;
}
int get(int x) {
  if (x == fa[x]) return x;
  return fa[x] = get(fa[x]);
}
int find(int a, int b) {
  int tmp = ((sum[b] - sum[a - 1]) % 1000000007 + 1000000007) % 1000000007;
  tmp = (long long)tmp * ny[a] % 1000000007;
  if (a != 1) tmp = (long long)tmp * 2 % 1000000007;
  return tmp;
}
int work(int a, int b) {
  int tmp = get(a);
  int ti = la[tmp], ans = (sa[get(b)] - sa[tmp]) % 1000000007;
  ans = (ans + 1000000007) % 1000000007;
  int tip = find(a, ti);
  if (a != 1) tip = (long long)tip * ny[1] % 1000000007;
  ans = (ans + tip) % 1000000007;
  return ans;
}
bool cmp2(const qus &A, const qus &B) { return A.id < B.id; }
int main() {
  scanf("%d%d", &n, &m);
  po[0] = ny[0] = 1;
  for (int i = 1; i <= n; i++)
    po[i] = (long long)po[i - 1] * 2 % 1000000007,
    ny[i] = poi(po[i], 1000000007 - 2);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]),
        sum[i] =
            (sum[i - 1] + (long long)po[i] * s[i] % 1000000007) % 1000000007;
  for (int i = 1; i <= m; i++) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + 1 + m, cmp);
  for (int i = 1; i <= n; i++) fa[i] = i, la[i] = i;
  int li = 0;
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      an[i] = s[i] * 2 % 1000000007,
      sa[i] = (s[i] * 2 % 1000000007 + sa[get(i - 1)]) % 1000000007,
      sa[i] = (sa[i] + 1000000007) % 1000000007;
    else
      an[i] = (s[i] + 1000000007) % 1000000007,
      sa[i] = (s[i] + 1000000007) % 1000000007;
    while (get(i) != 1) {
      int tmp = get(i);
      if (flag[tmp] == 1 || s[tmp] >= 0) {
        int ti = get(tmp - 1);
        if (s[tmp] != 0) {
          if (tmp - ti >= 30)
            flag[ti] = 1,
            s[ti] = (s[ti] + (long long)s[tmp] * po[tmp - ti] % 1000000007) %
                    1000000007,
            s[ti] = (s[ti] + 1000000007) % 1000000007;
          else
            s[ti] = s[ti] + (long long)s[tmp] * po[tmp - ti];
        }
        if (flag[tmp] == 1) flag[ti] = 1;
        if (s[ti] >= 1e9) flag[ti] = 1;
        if (flag[ti] == 1)
          s[ti] = (s[ti] % 1000000007 + 1000000007) % 1000000007;
        fa[tmp] = ti;
        an[ti] = find(ti, i);
        sa[ti] = (an[ti] + sa[get(ti - 1)]) % 1000000007;
        la[ti] = i;
      } else
        break;
    }
    while (li <= m && q[li + 1].r == i) {
      li++;
      q[li].ans = work(q[li].l, i);
    }
  }
  sort(q + 1, q + 1 + m, cmp2);
  for (int i = 1; i <= m; i++) {
    printf("%d\n", q[i].ans);
  }
  return 0;
}
