#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5, Mod = 1e9 + 7;
int n, q, a[N], suf[N], pre[N], fa[N], val[N], s[N], pw[N], ans[N];
struct questions {
  int l, r, id;
} que[N];
bool cmp(questions a, questions b) { return a.r < b.r; }
int father(int x) { return fa[x] == x ? x : fa[x] = father(fa[x]); }
void merge(int x, int y) {
  fa[x] = y, pre[y] = pre[x];
  int len = x - pre[x];
  if ((len > 30 && s[y] > 0) || 1LL * s[x] + ((1LL * s[y]) << len) > Mod)
    s[y] = Mod;
  else
    s[y] = (1LL * s[x] + ((1LL * s[y]) << len)) % Mod;
}
int calc(int l, int r) {
  return ((1LL * suf[l] - 1LL * suf[r + 1] * pw[r - l + 1]) % Mod + Mod) % Mod;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), s[i] = a[i], pre[i] = i - 1, fa[i] = i;
  for (int i = 1; i <= q; ++i)
    scanf("%d%d", &que[i].l, &que[i].r), que[i].id = i;
  sort(que + 1, que + 1 + q, cmp);
  for (int i = n; i; --i) suf[i] = (2LL * suf[i + 1] + a[i]) % Mod;
  pw[0] = 1;
  for (int i = 1; i <= n; ++i) pw[i] = ((1LL * pw[i - 1]) << 1) % Mod;
  for (int i = 1, j = 0; i <= n; ++i) {
    for (; pre[i] && s[i] >= 0; merge(pre[i], i))
      ;
    val[i] = (1LL * val[pre[i]] + ((1LL * calc(pre[i] + 1, i)) << 1)) % Mod;
    while (i == que[j + 1].r) {
      int x = father(que[++j].l);
      ans[que[j].id] =
          ((1LL * val[i] - 1LL * val[x] + 1LL * calc(que[j].l, x)) % Mod +
           Mod) %
          Mod;
    }
  }
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}
