#include <iostream>
#include <vector>

using namespace std;

int main() {
    int N;
    cin >> N;
    while (N--) {
        int T;
        cin >> T;
        vector<int> v(T, 0);
        vector<int> diff1(T/2 + 1, 0), diff2(T/2 + 1, 0);
        int sum = 0;
        for (int i = 0; i < T; i++) {
            cin >> v[i];
            if (i % 2 == 0) {
                sum += v[i];
            }
        }
        for (int i = 0; i <= T/2; i++) {
            if (2*i+1 < T) {
                diff1[i] += v[2*i+1] - v[2*i];
            }
            if (i > 0 && 2*i < T) {
                diff2[i-1] += v[2*i - 1] - v[2*i];
            }
        }
        int catch1 = 0, catch2 = 0, max1 = 0, max2 = 0;
        for (int i = 0; i < T/2 + 1; i++) {
            catch1 += diff1[i];
            catch2 += diff2[i];
            if (catch1 < 0) {
                catch1 = 0;
            }
            if (catch2 < 0) {
                catch2 = 0;
            }
            max1 = max(catch1, max1);
            max2 = max(catch2, max2);
        }
        cout << sum + max(max1, max2) << endl;
    }
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, -1, 1000001);
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == -1) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] < 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << "[";
        for (int i = 1; i < s.size(); i++) {
            if (first[i] < 0) {
                break;
            }
            cout << first[i] + ",";
        }
        cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, -1, 1000001);
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == -1) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] < 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << "[";
        for (int i = 0; i < s.size(); i++) {
            if (first[i] < 0) {
                break;
            }
            cout << first[i] + ",";
        }
        cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    int N;
    cin >> N;
    while (N--){
        memset(first, 0, 1000001 * sizeof(int));
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i <= s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << endl;
        //for (int i = 1; i < 100; i++) {
          //  cout << first[i] << ",";
        //}
        //cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    int N;
    cin >> N;
    while (N--){
        memset(first, 0, 1000001 * sizeof(int));
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        long long count = 0;
        for (int i = 0; i <= s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << endl;
        //for (int i = 1; i < 100; i++) {
          //  cout << first[i] << ",";
        //}
        //cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, -1, 1000001);
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum <= 0) {
                if (first[-sum] == -1) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] < 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int last[10000000];
    memset(last, 0, 10000000);
    string s;
    cin >> s;
    int sum = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '+') {
            sum++;
        } else {
            sum--;
        }
        if (sum < 0) {
            for (int j = 0; j < -sum; j++) {
                last[j] = i + 1;
            }
        }
    }
    int count = 0;
    for (int i = 0; i < s.size(); i++) {
        count += last[i];
        if (last[i] == 0) {
            break;
        }
    }
    return count;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    int N;
    cin >> N;
    while (N--){
        memset(first, 0, 1000001 * sizeof(int));
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << "[";
        for (int i = 1; i < 100; i++) {
            cout << first[i] + ",";
        }
        cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000009];
    int N;
    cin >> N;
    while (N--){
        memset(first, 0, 1000009 * sizeof(int));
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        long long count = 0;
        for (int i = 0; i <= s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << endl;
        //for (int i = 1; i < 100; i++) {
          //  cout << first[i] << ",";
        //}
        //cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, -1, 1000001);
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == -1) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] < 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, 0, 1000001 * sizeof(int));
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << "[";
        for (int i = 1; i < s.size(); i++) {
            if (first[i] == 0) {
                break;
            }
            cout << first[i] + ",";
        }
        cout << "]" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int last[1000001];
    memset(last, 0, 1000001);
    int N;
    cin >> N;
    while (N--){
    string s;
    cin >> s;
    int sum = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '+') {
            sum++;
        } else {
            sum--;
        }
        if (sum < 0) {
            for (int j = 0; j < -sum; j++) {
                last[j] = i + 1;
            }
        }
    }
    int count = 0;
    for (int i = 0; i < s.size(); i++) {
        count += last[i];
        if (last[i] == 0) {
            break;
        }
    }
    cout << count << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main() {
    int last[10000000];
    memset(last, last+10000000, 0);
    string s;
    cin >> s;
    int sum = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '+') {
            sum++;
        } else {
            sum--;
        }
        if (sum < 0) {
            for (int j = 0; j < -sum; j++) {
                last[j] = i + 1;
            }
        }
    }
    int count = 0;
    for (int i = 0; i < s.size(); i++) {
        count += last[i];
        if (last[i] == 0) {
            break;
        }
    }
    return count;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    memset(first, -1, 1000001);
    int N;
    cin >> N;
    while (N--){
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == -1) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = s.length();
        for (int i = 1; i < s.size(); i++) {
            if (last[i] < 0) {
                break;
            }
            count += last[i];
        }
        cout << count << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int last[1000001];
    memset(last, 0, 1000001);
    string s;
    cin >> s;
    int sum = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '+') {
            sum++;
        } else {
            sum--;
        }
        if (sum < 0) {
            for (int j = 0; j < -sum; j++) {
                last[j] = i + 1;
            }
        }
    }
    int count = 0;
    for (int i = 0; i < s.size(); i++) {
        count += last[i];
        if (last[i] == 0) {
            break;
        }
    }
    return count;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            if(s[flag]=='-') sum++;
            else sum--;
            flag--;
            vector<int> a;
            int minn=1;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(sum<minn)
                {
                    minn=sum;
                    if(!a.empty()) a.push_back(a[a.size()-1]+1);
                    else a.push_back(-sum-1);
                }
                else
                {
                    if(s[i]=='+' && sum<=0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            flag--;
            if(s[flag]=='-') sum++;
            else sum--;
            vector<int> a;
            int minn=1;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(a.empty()) a.push_back(-sum-1);
                else if(a[a.size()-1]+sum<-1)
                {
                    a.push_back(a[a.size()-1]+1);
                }
                else
                {
                    if(s[i-1]=='-' && s[i]=='+' && sum<=0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            flag--;
            if(s[flag]=='-') sum++;
            else sum--;
            vector<int> a;
            int minn=1;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(sum<minn)
                {
                    minn=sum;
                    if(!a.empty()) a.push_back(a[a.size()-1]+1);
                    else a.push_back(-sum-1);
                }
                else
                {
                    if(s[i-1]=='-' && s[i]=='+' && sum<=0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            if(s[flag]=='-') sum++;
            else sum--;
            flag--;
            vector<int> a;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(sum<0) a.push_back(max(-sum-1,a[a.size()-1]));
                else a.push_back(a[a.size()-1]);
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i;
                }
            }
            if(sum<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        ll len=s.length();
        ll flag=0;
        ll sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            ll temp=0;
            flag--;
            if(s[flag]=='-') sum++;
            else sum--;
            vector<ll> a;
            ll minn=1;
            for(ll i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(a.empty()) a.push_back(-sum-1);
                else if(a[a.size()-1]+sum<-1)
                {
                    a.push_back(a[a.size()-1]+1);
                }
                else
                {
                    if(s[i]=='+') temp=1;
                    else temp=-1;
                    if(sum+a[a.size()-1]-temp<0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            ll ans=0;
            for(ll i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            flag--;
            if(s[flag]=='-') sum++;
            else sum--;
            vector<int> a;
            int minn=1;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(sum<minn)
                {
                    minn=sum;
                    if(!a.empty()) a.push_back(a[a.size()-1]+1);
                    else a.push_back(-sum-1);
                }
                else
                {
                    if(s[i]=='+' && sum<=0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


/* Author: EndlessK
 * Time: 2020-10-20 10:04:52
**/
#include<bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.length();
        int flag=0;
        int sum=0;
        while(flag<len && sum>=0)
        {
            if(s[flag]=='-') sum--;
            else sum++;
            flag++;
        }
        if(flag==len)
        {
            if(sum>=0) cout<<len<<'\n';
            else cout<<2*len<<'\n';
        }
        else
        {
            int temp=0;
            flag--;
            if(s[flag]=='-') sum++;
            else sum--;
            vector<int> a;
            int minn=1;
            for(int i=flag;i<len;i++)
            {
                if(s[i]=='-') sum--;
                else sum++;
                if(a.empty()) a.push_back(-sum-1);
                else if(a[a.size()-1]+sum<-1)
                {
                    a.push_back(a[a.size()-1]+1);
                }
                else
                {
                    if(s[i]=='+') temp=1;
                    else temp=-1;
                    if(sum+a[a.size()-1]-temp<0) a.push_back(a[a.size()-1]+1);
                    else a.push_back(a[a.size()-1]);
                }
            }
            a.push_back(1e9);
            int ans=0;
            for(int i=0;i<a.size()-1;i++)
            {
                if(a[i]==a[i+1]) continue;
                else
                {
                    ans+=flag+i+1;
                }
            }
            if(sum+a[a.size()-2]<0) ans+=len;
            cout<<ans<<'\n';
        }

    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    int res = 0;
    int init = 0;
    bool f = true;
    while(f)
    {
        int cur = init;
        bool ok = true;
        for(int i = 0; i < s.length(); i++)
        {
            res++;
            if(s[i] == '+')
            {
                cur++;
            }
            else
            {
                cur--;
            }
            if(cur < 0)
            {
                 ok = false;
                 break;
            }
        }
        if(ok)
        {
            break;
        }
        init++;
    }
    cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}
/*int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else
        {
            g--;
        }
        if(g < 0)
        {
            g = 0;
            ans += (i+1);
        }
    }
    cout<<ans<<endl;*/


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else
        {
            g--;
        }
        if(g < 0)
        {
            g = 0;
            ans += (i+1);
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else if(g > 0)
        {
            g--;
        }
        else
        {
            ans+=(i+1);
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    ll res = 0;
    int init = 0;
    bool f = true;

        int cur = init;
        bool ok = true;
        for(int i = 0; i < s.length(); i++)
        {
            res++;
            if(s[i] == '+')
            {
                cur++;
            }
            else
            {
                cur--;
            }
            if(cur < 0)
            {
                 cur = 0;
                 res += (i+1);
            }
        }

    cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}
/*int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else
        {
            g--;
        }
        if(g < 0)
        {
            g = 0;
            ans += (i+1);
        }
    }
    cout<<ans<<endl;*/


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    int res = 0;
    int init = 0;
    bool f = true;

        int cur = init;
        bool ok = true;
        for(int i = 0; i < s.length(); i++)
        {
            res++;
            if(s[i] == '+')
            {
                cur++;
            }
            else
            {
                cur--;
            }
            if(cur < 0)
            {
                 cur = 0;
                 res += (i+1);
            }
        }

    cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}
/*int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else
        {
            g--;
        }
        if(g < 0)
        {
            g = 0;
            ans += (i+1);
        }
    }
    cout<<ans<<endl;*/


#include <bits/stdc++.h>

using namespace std;

#define pb pusb_back
typedef long long ll;
typedef vector < int > vi;

void run()
{
    string s;
    cin >> s;
    int g = 0;
    int ans = s.length();
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+')
        {
            g++;
        }
        else if(g != 0)
        {
            g--;
        }
        else
        {
            ans+=(i+1);
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t > 0)
    {
        run();
        t--;
    }
    return 0;
}


#include<iostream>
#define int long long int
using namespace std;
signed main(){
	int t;
	//ac
	scanf("%lld",&t);
	//ac
	while(t--){
		//ac
		string s;
		//ac
		cin>>s;
		//ac
		int ok=0;
		//ac
		int a[100005]={0},sum=0;
		//ac
		for(int i=0;i<s.size();i++){
			//ac
			if(ok==0&&s[i]=='-')
				a[i]=i+1;
			else//ac
				if(s[i]=='+')ok++;//ac
				else ok--;//ac
		}//ac
		a[s.size()]=s.size();//ac
		for(int i=0;i<=s.size();i++){//ac
			sum+=a[i]; //ac
		}//ac
		printf("%lld\n",sum);//ac
	}//ac
	return 0;//ac
}//ac


#include<iostream>
#define int long long
using namespace std;
signed main(){
	int t;
	//ac
	cin>>t;
	//ac
	while(t--){
		//ac
		string s;
		//ac
		cin>>s;
		//ac
		int ok=0;
		//ac
		int a[100005]={0},sum=0;
		//ac
		for(int i=0;i<s.size();i++){
			//ac
			if(ok==0&&s[i]=='-')
				a[i]=i+1;
				//ac
			else//ac
				if(s[i]=='+')ok++;//ac
				else ok--;//ac
		}//ac
		a[s.size()]=s.size();//ac
		for(int i=0;i<=s.size();i++){//ac
			sum+=a[i]; //ac
		}//ac
		cout<<sum<<endl;//ac
	}//ac
	return 0;//ac
}//ac


#include<iostream>
#define inf 1e9
using namespace std;
int main(){
	int t;
	cin>>t;
	int sum=0,ans=0;
	bool ss;
	string s;
	while(t--){
		cin>>s;
		sum=0;
		for(int i=0;i<=inf;i++){
			ans=i;
			ss=true;
			for(int i=0;i<s.size();i++){
				sum+=1;
				if(s[i]=='+'){
					ans+=1;
				}else{
					ans-=1;
				}
				if(ans<0){
					ss=false;
					break;
				}
			}
			if(ss)break;
		}
		printf("%d",sum);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#define int long long int
using namespace std;
signed main(){
	int t;
	//ac
	scanf("%lld",&t);
	//ac
	while(t--){
		//ac
		string s;
		//ac
		cin>>s;
		//ac
		int ok=0;
		//ac
		int a[1000005]={0},sum=0;
		//ac
		for(int i=0;i<s.size();i++){
			//ac
			if(ok==0&&s[i]=='-')
				a[i]=i+1;
			else{
			//ac
				if(s[i]=='+')ok++;//ac
				else ok--;//ac
			}
		}//ac
		a[s.size()]=s.size();//ac
		for(int i=0;i<=s.size();i++){//ac
			sum+=a[i]; //ac
		}//ac
		printf("%lld\n",sum);//ac
	}//ac
	return 0;//ac
}//ac


#include<iostream>
#include<cstdio>
#define int long long int
using namespace std;
signed main(){
	int t;
	//ac
	scanf("%lld",&t);
	//ac
	while(t--){
		//ac
		string s;
		//ac
		cin>>s;
		//ac
		int ok=0;
		//ac
		int a[100005]={0},sum=0;
		//ac
		for(int i=0;i<s.size();i++){
			//ac
			if(ok==0&&s[i]=='-')
				a[i]=i+1;
			else{
			//ac
				if(s[i]=='+')ok++;//ac
				else ok--;//ac
			}
		}//ac
		a[s.size()]=s.size();//ac
		for(int i=0;i<=s.size();i++){//ac
			sum+=a[i]; //ac
		}//ac
		printf("%lld\n",sum);//ac
	}//ac
	return 0;//ac
}//ac


#include<iostream>
#define int long long
#define inf 1e9
using namespace std;
signed main(){
	int t;
	cin>>t;
	int sum=0,ans=0;
	bool ss;
	string s;
	while(t--){
		cin>>s;
		sum=0;
		for(int i=0;i<=inf;i++){
			ans=i;
			ss=true;
			for(int i=0;i<s.size();i++){
				sum+=1;
				if(s[i]=='+'){
					ans+=1;
				}else{
					ans-=1;
				}
				if(ans<0){
					ss=false;
					break;
				}
			}
			if(ss)break;
		}
		printf("%d\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int sum[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,cur=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='-') cur--;
            else
            {
                if(cur<0) cur=1;
                else cur++;
            }
            sum[i]=cur;
        }
        for(int i=1;i<=n;i++)
        {
            if(sum[i]<0) res+=i;
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,now=0;
        while(1)
        {
            int cur=now,ok=1;
            for(int i=1;i<=n;i++)
            {
                res=res+1;
                if(s[i]=='+') cur=cur+1;
                else cur=cur-1;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok) break;
            now++;
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,cur=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='-') cur--;
            else cur++;
            if(cur<0) res+=i,cur=0;
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int sum[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,cur=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='-') cur--;
            else
            {
                if(cur<0) cur=i;
                else cur++;
            }
            sum[i]=cur;
        }
        for(int i=1;i<=n;i++)
        {
            if(sum[i]<0) res+=i;
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>(s+1);
        int n=strlen(s+1);
        ll res=0,cur=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='-') cur--;
            else cur++;
            if(cur<0)
            {
                cur=0;
                res+=i;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,cur=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='-') cur--;
            else cur++;
            if(cur<0)
            {
                cur=0;
                res+=i;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+10;
char s[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>(s+1);
        int n=strlen(s+1);
        int res=0,now=1;
        while(s[now]=='-'&&now<=n) res+=now,now++;
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<cstdio>

using namespace std;

int main()
{
	int t,res,init,cur,ok;
	scanf("%d",&t);

	while(t--)
	{
		char s[1000000];
		scanf("%s",s);
		res=0;
		for(init=0;init<=10000000;init++)
		{
			cur=init;
			ok=1;
			for(int i=0;s[i]!='\0';i++)
			{
				res+=1;
				if(s[i]=='+')
				cur+=1;
				else
				cur-=1;

				if(cur<0)
				{
					ok=0;
					break;
				}
			}
			if(ok==1)
			break;
		}
		printf("%d\n",res);
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		vector <long long int> pi;
		vector <long long int> ni;
		vector <long long int> pn;

		long long int p=0,n=0,len=0;
		char s[1000001];
		cin>>s;
		for(long long int i=0;s[i]!='\0';i++)
		{
			if(s[i]=='+')
			p++;
			else
			n++;
			pi.push_back(p);
			ni.push_back(n);
			pn.push_back(p-n);
			len++;
		}

		long long int res=0,j=0;
		for(long long int i=0;i<n && j<len;j++)
		{
			while(i<n && i+1+pn[j]==0 && j<len)
			{

				res+=pi[j]+ni[j];
				i++;
				j++;

			}
		}

		cout<<res+len<<endl;
	}

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		vector <long long int> pi;
		vector <long long int> ni;
		vector <long long int> pn;

		long long int p=0,n=0,len=0;
		char s[1000001];
		cin>>s;
		for(int i=0;s[i]!='\0';i++)
		{
			if(s[i]=='+')
			p++;
			else
			n++;
			pi.push_back(p);
			ni.push_back(n);
			pn.push_back(p-n);
		//	cout<<pi[i]<<" "<<ni[i]<<endl;
			len++;
		}
//	cout<<s<<endl;

		int res=0,j=0;
		for(int i=0;i<n && j<len;j++)
		{
			while(i<n && i+1+pn[j]==0 && j<len)
			{
				//if(i+1+pn[j]==0)
				//{
				//	cout<<pi[j]<<" "<<ni[j]<<endl;
					res+=pi[j]+ni[j];
					i++;
				//}
				j++;

			}
		}

		cout<<res+len<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		long long int rslt, len, tmp, i;
		cin>>s;
		len=s.length();
		rslt=len;
		tmp=0;
		for(i=0;i<len;i++)
		{
			if(s[i]=='+')
			tmp+=1;
			else
			tmp-=1;
			if(tmp==-1)
			{
				rslt+=i+1;
				tmp=0;
			}
		}
		cout<<rslt<<endl;
	}
	return 0;
}


#include<cstdio>

using namespace std;

int main()
{
	int t,res,init,cur,ok;
	scanf("%d",&t);

	while(t--)
	{
		char s[1000001];
		scanf("%s",s);
		res=0;
		for(init=0;1;init++)
		{
			cur=init;
			ok=1;
			for(int i=0;s[i]!='\0';i++)
			{
				res+=1;
				if(s[i]=='+')
				cur+=1;
				else
				cur-=1;

				if(cur<0)
				{
					ok=0;
					break;
				}
			}
			if(ok==1)
			break;
		}
		printf("%d\n",res);
	}
	return 0;
}


#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int t,res,init,cur,ok;
	cin>>t;
	getchar();

	while(t--)
	{
		char s[1000000];
		scanf("%[^\n]",s);
		getchar();
		res=0;
		for(init=0;1;init++)
		{
			cur=init;
			ok=1;
			for(int i=0;s[i]!='\0';i++)
			{
				res+=1;
				if(s[i]=='+')
				cur+=1;
				else
				cur-=1;

				if(cur<0)
				{
					ok=0;
					break;
				}
			}
			if(ok==1)
			break;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		vector <long long int> pi;
		vector <long long int> ni;
		vector <long long int> pn;

		long long int p=0,n=0,len=0;
		char s[1000001];
		cin>>s;
		for(long long int i=0;s[i]!='\0';i++)
		{
			if(s[i]=='+')
			p++;
			else
			n++;
			pi.push_back(p);
			ni.push_back(n);
			pn.push_back(p-n);
			len++;
		}

		int res=0,j=0;
		for(long long int i=0;i<n && j<len;j++)
		{
			while(i<n && i+1+pn[j]==0 && j<len)
			{

				res+=pi[j]+ni[j];
				i++;
				j++;

			}
		}

		cout<<res+len<<endl;
	}

	return 0;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
string row;
int l,r;
vector<int> preSum(maxn);
map<int,ll> mapping;
inline ll pow(int power);
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>q;
    cin>>row;

    preSum[1]=row[0]-'0';
    rep(i,2,n+1)
        switch(row[i-1]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    mapping[0]=1;

    while(q--){
        ll ans=0;
        cin>>l>>r;
        int n1=preSum[r]-preSum[l]+row[l-1]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow(n1)-1)%mod;
        int n2=r-l+1-n1;                            //arithmetic sequence
//        cout<<"n2 is:"<<n2<<endl;
        ll a1=ans,d=ans;
        ans+=((ll)n2*a1+(n2*1LL*(n2-1)/2)*d)%mod;
        cout<<ans<<endl;
    }
    re 0;
}
inline ll pow(int power){
    if(mapping.find(power)!=mapping.end())
        re mapping[power];

    ll ans=1;
    while(power--){
        ans*=2;
        ans%=mod;
    }

    re mapping[power]=ans;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
char row[maxn];
int l,r;
vector<ll> pow(maxn);
vector<int> preSum(maxn);
map<int,ll> mapping;
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",row+1);

    preSum[1]=row[1]-'0';
    rep(i,2,n+1)
        switch(row[i]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    pow[0]=1;
    rep(i,1,maxn){
        pow[i]=pow[i-1]*2;
        pow[i]%=mod;
    }

    while(q--){
        ll ans=0;
        scanf("%d %d",&l,&r);

        int n1=preSum[r]-preSum[l]+row[l]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow[n1]-1)%mod;

        int n2=r-l+1-n1;                            //arithmetic sequence
        ll a1=ans;
//        cout<<"n2 is:"<<n2<<endl;
        ans=(ans+a1*(pow[n2]-1))%mod;
        printf("%lld\n",ans);
    }
    re 0;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
char row[maxn];
int l,r;
vector<ll> pow(maxn);
vector<int> preSum(maxn);
map<int,ll> mapping;
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",row+1);

    preSum[1]=row[1]-'0';
    rep(i,2,n+1)
        switch(row[i]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    pow[0]=1;
    rep(i,1,maxn){
        pow[i]=pow[i-1]*2;
        pow[i]%=mod;
    }

    while(q--){
        ll ans=0;
        scanf("%d %d",&l,&r);

        int n1=preSum[r]-preSum[l]+row[l]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow[n1]-1)%mod;

        int n2=r-l+1-n1;                            //arithmetic sequence
//        cout<<"n2 is:"<<n2<<endl;
        ll a1=ans,d=ans;
        ans+=(1LL*n2*a1+(n2*1LL*(n2-1)/2)*d)%mod;
        printf("%lld\n",ans);
    }
    re 0;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
char row[maxn];
int l,r;
vector<ll> pow(maxn);
vector<int> preSum(maxn);
map<int,ll> mapping;
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",row+1);

    preSum[1]=row[1]-'0';
    rep(i,2,n+1)
        switch(row[i]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    pow[0]=1;
    rep(i,1,maxn){
        pow[i]=pow[i-1]*2;
        pow[i]%=mod;
    }

    while(q--){
        ll ans=0;
        scanf("%d %d",&l,&r);

        int n1=preSum[r]-preSum[l]+row[l]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow[n1]-1)%mod;

        int n2=r-l+1-n1;                            //arithmetic sequence
//        cout<<"n2 is:"<<n2<<endl;
        ans+=(7*(pow[n2]-1)*1LL)%mod;
        printf("%lld\n",ans);
    }
    re 0;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
char row[maxn];
int l,r;
vector<int> preSum(maxn);
map<int,ll> mapping;
inline ll pow(int power);
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",row+1);

    preSum[1]=row[1]-'0';
    rep(i,2,n+1)
        switch(row[i]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    mapping[0]=1;

    while(q--){
        ll ans=0;
        scanf("%d %d",&l,&r);
        int n1=preSum[r]-preSum[l]+row[l-1]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow(n1)-1)%mod;
        int n2=r-l+1-n1;                            //arithmetic sequence
//        cout<<"n2 is:"<<n2<<endl;
        ll a1=ans,d=ans;
        ans+=((ll)n2*a1+(n2*1LL*(n2-1)/2)*d)%mod;
        printf("%lld\n",ans);
    }
    re 0;
}
inline ll pow(int power){
    if(mapping.find(power)!=mapping.end())
        re mapping[power];

    ll ans=1;
    while(power--){
        ans*=2;
        ans%=mod;
    }

    re mapping[power]=ans;
}


#include <stdio.h>
#include <climits>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <utility>
#define INF 0x3f3f3f3f
#define re return
#define ll long long
#define Pair pair<int,int>
#define Push(a) Push_back(a)
#define Make(a,b) make_pair(a,b);
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>+star;index--)
using namespace std;
const int maxn=1e6+5;
const ll mod=1e9+7;

int n,q;
char row[maxn];
int l,r;
vector<int> preSum(maxn);
map<int,ll> mapping;
inline ll pow(int power);
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",row+1);

    preSum[1]=row[1]-'0';
    rep(i,2,n+1)
        switch(row[i]){
            case '0':
                preSum[i]=preSum[i-1];
                break;
            case '1':
                preSum[i]=preSum[i-1]+1;
        }
    mapping[0]=1;

    while(q--){
        ll ans=0;
        scanf("%d %d",&l,&r);

        int n1=preSum[r]-preSum[l]+row[l]-'0';    //equal ratio series
//        cout<<"n1 is:"<<n1<<endl;
//        cout<<"pow(2,n1) is:"<<pow(n1)<<endl;
        ans+=(pow(n1)-1)%mod;

        int n2=r-l+1-n1;                            //arithmetic sequence
//        cout<<"n2 is:"<<n2<<endl;
        ll a1=ans,d=ans;
        ans+=((ll)n2*a1+(n2*1LL*(n2-1)/2)*d)%mod;
        printf("%lld\n",ans);
    }
    re 0;
}
inline ll pow(int power){
    if(mapping.find(power)!=mapping.end())
        re mapping[power];

    ll ans=1;
    while(power--){
        ans*=2;
        ans%=mod;
    }

    re mapping[power]=ans;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                ans += pow(ull(2),one-1);
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				b %= MOD;
                ans2 += pow(ull(2),zero-1);
				ans2 %= MOD;
                ans2 *= b;
                ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i < 100004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                //ans += pow(ull(2),one-1);
                ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;
using namespace std;

const ll MOD = 1e9 + 7;
ll poww[100005],arr[100005];
vector <int> onepos;
string s;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    poww[0] = 1;
    for(int i = 1; i < 100005; i++) poww[i] = (poww[i-1] * 2 ) % MOD;
    int n,t;
    cin >> n >> t >> s;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
        else arr[i+1] = arr[i];
    }
    for(int i = 0; i < t; i++)
    {
        ll l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
        cin >> l >> r;
        int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
        if(index < onepos.size() && onepos[index] <= r)
        {
            one += arr[r] - arr[l];
            if(arr[l] > arr[l-1]) one++;
        }
        zero += max((ll)0,(r - l + 1) - one);
        ans += poww[one] - 1;
        ans2 += ans * (poww[zero] - 1);
        ans2 %= MOD;
        cout << (ans+ans2) % MOD << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i < 100004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				//one %= 30;
                //ans += pow(ull(2),one-1);
                ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				//zero %= 30;
				b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                ans += pow(2,one-1);
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
                ans2 += b * pow(2,zero-1);
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i <= 100005; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				//one %= 30;
                //ans += pow(ull(2),one-1);
                ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				//zero %= 30;
				b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[200005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        poww[0] = 1;
        for(int i = 1; i < 200004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
//            if(one)
//            {
				ans += poww[one] - 1;
//            }
            ans %= MOD;
//            if(zero)
//            {
                ans2 += ans * (poww[zero] - 1);
                ans2 %= MOD;
//            }
            //ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i < 100004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                //ans += pow(ull(2),one-1);

                if(one > 0) ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				//b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                if(zero > 0) ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i < 100004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                //ans += pow(ull(2),one-1);

                if(one > 0) ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                if(zero > 0) ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
                ans += pow(2,one-1);
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
                ans2 += b * pow(2,zero-1);
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                ans += pow(2,one-1);
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				b %= MOD;
                ans2 += pow(2,zero-1);
				ans2 %= MOD;
				ans2 *= b;
				ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				one %= 30;
                ans += pow(2,one-1);
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				b %= MOD;
                ans2 += b * pow(2,zero-1);
				ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define lb lower_bound
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
const ll MOD = 1e9 + 7;
ull poww[100005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q;
    //cin >> q;
    q = 1;
    while(q--)
    {
      poww[0] = 1;
        for(int i = 1; i < 100004; i++) poww[i] = (poww[i-1] * 2 ) % MOD;

        vector <int> onepos;
        string s;
        int n,t;
        ull arr[100005] = {};
        cin >> n >> t >> s;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') {arr[i+1] = arr[i] + 1; onepos.pb(i+1);}
            else arr[i+1] = arr[i];
        }
        for(int i = 0; i < t; i++)
        {
            ull l,r,one = 0,zero = 0,ans = 0,ans2 = 0,a = 0,b = 0;
            cin >> l >> r;
            if(l == r)
            {
                if(arr[r] > arr[r-1]) one = 1;
            }
            else
            {
                int index = lb(onepos.begin(),onepos.end(),l) - onepos.begin();
                if(index < onepos.size() && onepos[index] <= r)
                {
                    one += arr[r] - arr[l];
                    if(arr[l] > arr[l-1]) one++;
                }
            }
            zero += (r - l + 1) - one;
            if(one)
            {
				//one %= 30;
                //ans += pow(ull(2),one-1);

                if(one > 0) ans += poww[one-1];
                a += max(ans - 1,(ull)0);
                b += ans + a;
                ans += a;
            }
            ans %= MOD;
            if(zero)
            {
				zero %= 30;
				//b %= MOD;
                //ans2 += pow(ull(2),zero-1);
                if(zero > 0) ans2 += poww[zero-1];
				ans2 %= MOD;
        ans2 *= b;
        ans2 %= MOD;
                ans2 += ans2 - b;
            }
            ans2 %= MOD;
            ans += ans2;
            cout << ans % MOD << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, q, l, r, br0, br1, x, res = 0;
	int prsum[100100];
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (int i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}
		for (int i = 0; i < br0; i++)
		{
			res = res * 2;
		}
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , iszag = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		iszag = res;
		res = 0;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
		{
			res = iszag * (power[br0] - 1) + iszag;
		}
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , iszag = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		iszag = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/

	    res = res * (power[br0] - 1) + iszag;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
    memset(prsum,0,sizeof(prsum));
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = res * (power[br0] - 1) + cc;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, q, l, r, br0, br1, x, res = 0;
	int prsum[100100];
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (int i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}
		if (br0 > 0)
			res = br0 * (res * 2);
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, xx, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	xx = 1;
	 for (int i=1;i<100100;i++){
        xx*=2LL;
        xx%=mod;
        power[i]=xx;
    }
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = res * (power[br0] - 1) + cc;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
    memset(prsum,0,sizeof(prsum));
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
		{
			res = res * (power[br0] - 1) + cc;
			res %= mod;
		}

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
    memset(prsum,0,sizeof(prsum));
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
		{
			res = res * (power[br0]);
			res %= mod;
		}

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		if (br0 > 0)
		{
			res = res * (power[br0] - 1) + cc;
			res %= mod;
		}

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	int n, q, l, r, br0, br1, x, res = 0;
	int prsum[100100];
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (int i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}
		for (int i = 0; i < br0; i++)
		{
			res = res * 2;
		}
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		if (br0 > 0)
		{
			res = res * (power[br0] - 1) + cc;
			res %= mod;
		}

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = res * (power[br0] - 1) + cc;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
	ll n, q, l, r, br0, br1, x, res = 0;
	ll prsum[100100];
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = pow(2, br1) - 1;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = br0 * (res * 2);
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, q, l, r, br0, br1, x, res = 0;
	int prsum[100100];
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (int i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = pow(2, br1) - 1;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = br0 * (res * 2);
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0, ccc=0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		while (br0--)
		{
			ccc = res--;
			res += ccc;
		}
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, xx, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	x = 1;
	 for (int i=1;i<100100;i++){
        xx*=2LL;
        xx%=mod;
        power[i]=xx;
    }
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = res * (power[br0] - 1) + cc;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , cc = 0;
ll prsum[100100], power[100100];

int main()
{
    memset(sum,0,sizeof(sum));
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		cc = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
			res = res * (power[br0] - 1) + cc;

		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 == 1)
			res = br0 * (res * 2);
		else if (br0 > 1)
			res = br0 * (res * 2) * 2;
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

ll n, q, l, r, br0, br1, x, res = 0 , iszag = 0;
ll prsum[100100], power[100100];

int main()
{
	power[0] = 1;
	for (int i = 1; i < 100100; i++)
	{
		power[i] = power[i - 1] *2;
		power[i] %= mod;
	}
	string s;
	cin >> n >> q;
	cin >> s;
	prsum[0] = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		prsum[i + 1] = prsum[i];
		if (s[i] == '1')
			prsum[i + 1]++;
	}
	while (q--)
	{
		cin >> l >> r;
		x = r - l + 1;
		br1 = prsum[r] - prsum[l - 1];
		br0 = x - br1;
		res = power[br1] - 1;
		iszag = res;
		/*for (int i = 0; i < br1; i++)
		{
			res += pow(2, i);
		}*/
		if (br0 > 0)
		{
			res = res * (power[br0] - 1) + iszag;
		}
		cout << res << endl;
		res = 0;
	}

	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int inf = 1e9 + 7;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	vector <int> L(n + 1);
	L[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i - 1] * 2 % inf;
	}
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] +1;
		}
		else
		{
			pref0[i] = pref0[i - 1];
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = L[b - a + 1], y;
		if(a == 1)
		{
			y = L[pref0[b - 1]];
		}
		else
		{
			y = L[pref0[b - 1] - pref0[a - 2]];
		}
		cout << (p - y + inf) % inf << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int inf = 1e9 + 7;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0' + 1;
	vector <int> L(n + 1);
	L[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i - 1] * 2 % inf;
	}
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = (pref0[i - 1] * 2 )% inf;
		}
		else
		{
			pref0[i] = pref0[i - 1];
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = L[b - a + 1], y;
		if(a == 1)
		{
			y = L[pref0[b - 1] - pref0[a - 1] + (pref0[0] == '0')];
		}
		else
		{
			y = L[(2, pref0[b - 1] - pref0[a - 2])];
		}
		cout << (p - y + inf) % inf << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int inf = 1e9 + 7;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	vector <int> L(n + 1);
	L[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i - 1] * 2 % inf;
	}
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] +1;
		}
		else
		{
			pref0[i] = pref0[i - 1];
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = L[b - a + 1], y;
		if(a == 1)
		{
			y = L[pref0[b - 1] - pref0[a - 1] + (pref0[0] == '0')];
		}
		else
		{
			y = L[(2, pref0[b - 1] - pref0[a - 2])];
		}
		cout << (p - y + inf) % inf << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	pref1[0] = h[0] - '0';
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] + 1;
			pref1[i] = pref1[i - 1];
		}
		else
		{
			pref0[i] = pref0[i - 1];
			pref1[i] = pref1[i - 1] + 1;
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		cout << pow(2, b - a + 1) - pow(2, pref0[b - 1] - pref0[a - 1]) << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int inf = 1e9 + 7;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	vector <int> L(n + 1);
	L[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i - 1] * 2 % inf;
	}
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] +1;
		}
		else
		{
			pref0[i] = pref0[i - 1];
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = L[b - a + 1], y;
		if(a == 1)
		{
			y = L[pref0[b - 1] - pref0[a - 1] + (pref0[0] == '0')];
		}
		else
		{
			y = L[pref0[b - 1] - pref0[a - 2]];
		}
		cout << (p - y + inf) % inf << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	pref1[0] = h[0] - '0';
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] + 1;
			pref1[i] = pref1[i - 1];
		}
		else
		{
			pref0[i] = pref0[i - 1];
			pref1[i] = pref1[i - 1] + 1;
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = (int)pow(2, b - a + 1) % (int)1000000007;
		int y = (int)pow(2, pref0[b - 1] - pref0[a - 1]) % (int)1000000007;
		cout << (p - y + 1000000007) % 1000000007 << endl;
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	string h;
	cin >> h;
	vector <int> pref0(n), pref1(n);
	pref0[0] = 1 - h[0] + '0';
	pref1[0] = h[0] - '0';
	for(int i = 1; i < n; i++)
	{
		if(h[i] == '0')
		{
			pref0[i] = pref0[i - 1] + 1;
			pref1[i] = pref1[i - 1];
		}
		else
		{
			pref0[i] = pref0[i - 1];
			pref1[i] = pref1[i - 1] + 1;
		}
	}
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		int p = (int)pow(2, b - a + 1) % (int)1000000007, y;
		if(a == 1)
		{
			y = (int)pow(2, pref0[b - 1] - pref0[a - 1] + (pref0[0] == '0')) % (int)1000000007;
		}
		else
		{
			y = (int)pow(2, pref0[b - 1] - pref0[a - 2]) % (int)1000000007;
		}
		cout << (p - y + 1000000007) % 1000000007 << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a,b) memset(a,(b),sizeof(a))
#define FOR(i,j,k,in) for(int i=j; i<k; i+=in)
#define RFOR(i,j,k,in) for(int i=j; i>=k; i-=in)
#define REP(i,j) FOR(i,0,j,1)
#define RREP(i,j) RFOR(i,j,0,1)
#define all(cont) cont.begin(),cont.end()
#define rall(cont) cont.end(),cont.begin()
#define FOREACH(it,l) for(auto it=l.begin(); it!=l.end(); it++)
#define IN(A,B,C) assert(B<=A&&A<=C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()

const double pi=acos(-1.0);

typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

template<typename T,typename U> inline void amin(T &x, U y) { if(y<x) x=y; }
template<typename T,typename U> inline void amax(T &x, U y) { if(x<y) x=y; }

template<typename T> inline void write(T x)
{
	int i=20;
	char buf[21];
	// buf[10]=0;
	buf[20]='\n';
	do {
		buf[--i]=x%10+'0';
		x/=10;
	} while(x);
	do putchar(buf[i]); while (buf[i++]!='\n');
}

template<typename T> inline T readInt()
{
	T n=0, s=1;
	char p=getchar();
	if(p=='-') s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-') p=getchar();
	if(p=='-') s=-1, p=getchar();
	while(p>='0'&&p<='9') {
		n=(n<<3)+(n<<1)+(p-'0');
		p=getchar();
	}
	return n*s;
}

int64 bigMod(int64 base, int64 exp, int64 m)
{
	if(exp==0) return 1;
	if(!(exp&1)) {
		int64 temp=bigMod(base,exp/2,m)%m;
		return (temp*temp)%m;
    }
	else return ((base%m)*(bigMod(base,exp-1,m))%m)%m;
}

int main()
{
	int64 n, q, l, r;
	string s;
	n=read(int64);
	int64 x[n+1], y[n+1];
	q=read(int64);
	cin>>s;
	x[0]=0;
	y[0]=0;
	if(s[0]=='1') {
		x[1]=1;
		y[1]=0;
	}
	else {
		x[1]=0;
		y[1]=1;
	}
	FOR(i,2,n+1,1) {
		if(s[i-1]=='1') {
			x[i]=x[i-1]+1;
			y[i]=y[i-1];
		}
		else {
			x[i]=x[i-1];
			y[i]=y[i-1]+1;
		}
	}
	REP(i,q) {
		l=read(int64);
		r=read(int64);
		cout<<(bigMod(2,x[r]-x[l-1],MOD)-1)*bigMod(2,y[r]-y[l-1],MOD)%MOD<<'\n';
	}
	return 0;
}