#include <bits/stdc++.h>
using namespace std;
int n, q, last;
const long long P = 1e9 + 7;
const long long PP = P * 2ll;
const int MAXA = 1e6;
long long mi[MAXA], inv[MAXA], suma[MAXA], sumb[MAXA], s[MAXA], summ[MAXA],
    a[MAXA], sum[MAXA], ans[MAXA];
int c[MAXA], l[MAXA], r[MAXA], st[MAXA], cnt[MAXA], fa[MAXA], top;
int ask(int x) { return (fa[x] == x) ? x : fa[x] = ask(fa[x]); }
long long mo(long long x) { return (x % P + P) % P; }
int mysort(int x, int y) { return r[x] < r[y]; }
int main() {
  int x, y;
  scanf("%d%d", &n, &q);
  inv[0] = 1;
  inv[1] = P - P / 2;
  mi[1] = 2;
  mi[0] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = inv[i - 1] * inv[1] % P, mi[i] = mi[i - 1] * mi[1] % P;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sumb[i] = (sumb[i - 1] + a[i] * mi[i - 1] % P) % P;
  }
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l[i], &r[i]);
    c[i] = i;
  }
  sort(c + 1, c + q + 1, mysort);
  last = 1;
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    cnt[i] = 1;
    s[++top] = a[i];
    summ[top] = mo(summ[top - 1] + s[top]);
    st[i] = top;
    y = i;
    while (y > 1 && s[top] >= 0ll) {
      x = ask(y - 1);
      top--;
      s[top] = s[top] + s[top + 1] * mi[cnt[x]];
      if (s[top] > P) s[top] = s[top] % P + P;
      summ[top] = (summ[top - 1] + s[top]) % P;
      cnt[x] += cnt[y];
      fa[y] = x;
      y = x;
    }
    while (last <= q && r[c[last]] == i) {
      x = ask(l[c[last]]);
      ans[c[last]] = mo((summ[top] - summ[st[x]]) * 2ll);
      ans[c[last]] =
          mo(ans[c[last]] + mo(sumb[x + cnt[x] - 1] - sumb[l[c[last]] - 1]) *
                                inv[l[c[last]] - 1] % P);
      last++;
    }
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
