#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)<<'\n';
	}
	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 f(int64 a, int64 b)
{
	int64 p1, p2;
	p1=1;
	p2=1;
	REP(i,a) p1*=2;
	REP(i,b) p2*=2;
	return (p1-1)*p2;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int64 n, q, X, *x, l, r, x1, x0;
	n=read(int64);
	q=read(int64);
	x=(int64*)malloc(n*sizeof(int64));
	X=read(int64);
	RREP(i,n-1) {
		x[i]=X%10;
		X/=10;
	}
	REP(i,q) {
		l=read(int64)-1;
		r=read(int64)-1;
		x1=0;
		x0=0;
		FOR(i,l,r+1,1)
			if(x[i]==1) x1++;
			else x0++;
		cout<<f(x1,x0)<<'\n';
	}
	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 f(int64 a, int64 b)
{
	int64 p1, p2;
	p1=1;
	p2=1;
	REP(i,a) {
		p1*=2;
		p1%=MOD;
	}
	REP(i,b) {
		p2*=2;
		p2%=MOD;
	}
	return (p1-1)*p2%MOD;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int64 n, q, X, *x, l, r, x1, x0;
	n=read(int64);
	q=read(int64);
	x=(int64*)malloc(n*sizeof(int64));
	X=read(int64);
	RREP(i,n-1) {
		x[i]=X%10;
		X/=10;
	}
	REP(i,q) {
		l=read(int64)-1;
		r=read(int64)-1;
		x1=0;
		x0=0;
		FOR(i,l,r+1,1)
			if(x[i]==1) x1++;
			else x0++;
		cout<<f(x1,x0)<<'\n';
	}
	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;
}

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

int main()
{
	int n, q, l, r;
	string s;
	n=read(int);
	int x[n+1], y[n+1];
	q=read(int);
	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(int);
		r=read(int);
		cout<<(bigMod(2,x[r]-x[l-1],MOD)-1)*bigMod(2,y[r]-y[l-1],MOD)<<'\n';
	}
	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 powerOf2(int64 b)
{
	int64 a;
	a=1;
	REP(i,b) a*=2;
	return a;
}

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<<(powerOf2(x[r]-x[l-1])-1)*powerOf2(y[r]-y[l-1])<<'\n';
	}
	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 powerOf2(int64 b)
{
	int64 a;
	a=1;
	REP(i,b) a*=2;
	return a;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int64 n, q, s, l, r;
	n=read(int64);
	int64 d[n+1], x[n+1], y[n+1];
	q=read(int64);
	s=read(int64);
	RFOR(i,n,1,1) {
		d[i]=s%10;
		s/=10;
	}
	x[0]=0;
	y[0]=0;
	if(d[1]==1) {
		x[1]=1;
		y[1]=0;
	}
	else {
		x[1]=0;
		y[1]=1;
	}
	FOR(i,2,n+1,1) {
		if(d[i]==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);
		write((powerOf2(x[r]-x[l-1])-1)*powerOf2(y[r]-y[l-1]));
	}
	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 f(int64 a, int64 b)
{
	int64 p1, p2;
	p1=1;
	p2=1;
	REP(i,a) {
		p1*=2;
		p1%=MOD;
	}
	REP(i,b) {
		p2*=2;
		p2%=MOD;
	}
	return (p1-1)*p2%MOD;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int64 n, q, X, *x, l, r, x1, x0;
	n=read(int64);
	q=read(int64);
	x=(int64*)malloc(n*sizeof(int64));
	X=read(int64);
	RREP(i,n-1) {
		x[i]=X%10;
		X/=10;
	}
	REP(i,q) {
		l=read(int64)-1;
		r=read(int64)-1;
		x1=0;
		x0=0;
		FOR(i,l,r+1,1)
			if(x[i]==1) x1++;
			else x0++;
		write(f(x1,x0));
	}
	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;
}

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<<(pow(2,x[r]-x[l-1])-1)*pow(2,y[r]-y[l-1])<<'\n';
	}
	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 f(int64 a, int64 b)
{
	int64 p1, p2;
	p1=1;
	p2=1;
	REP(i,a) p1*=2;
	REP(i,b) p2*=2;
	return (p1-1)*p2;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int64 n, q, X, *x, l, r, x1, x0;
	n=read(int64);
	q=read(int64);
	x=(int64*)malloc(n*sizeof(int64));
	X=read(int64);
	RREP(i,n-1) {
		x[i]=X%10;
		X/=10;
	}
	REP(i,q) {
		l=read(int64)-1;
		r=read(int64)-1;
		x1=0;
		x0=0;
		FOR(i,l,r+1,1)
			if(x[i]==1) x1++;
			else x0++;
		write(f(x1,x0));
	}
	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;
}

int f(int a, int b)
{
	int p1, p2;
	p1=1;
	p2=1;
	REP(i,a) p1*=2;
	REP(i,b) p2*=2;
	return (p1-1)*p2;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, q, X, *x, l, r, x1, x0;
	n=read(int);
	q=read(int);
	x=(int*)malloc(n*sizeof(int));
	X=read(int);
	RREP(i,n-1) {
		x[i]=X%10;
		X/=10;
	}
	REP(i,q) {
		l=read(int)-1;
		r=read(int)-1;
		x1=0;
		x0=0;
		FOR(i,l,r+1,1)
			if(x[i]==1) x1++;
			else x0++;
		write(f(x1,x0));
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)

using namespace std;
ll int d[500005];
ll int dp[500005];
void solve()
{
	ll int n;
	cin>>n;
	d[1]=0;
	d[2]=3;
	for(ll int i=3;i<=n;i++)
	{
		d[i]=d[i-1]%hell*2;

	}
	dp[2]=3;

	dp[1]=0;
	ll int k;
	for(ll int i=3;i<=n;i++)
	{
		k=i;
		for(ll j=2;j<i;j++)
		{

			dp[i]=(dp[i]%hell+dp[j]%hell*dp[k-j]%hell)%hell;
		}
		dp[i]=(d[i]%hell+dp[i]%hell)%hell;
	}
	cout<<dp[n];

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
using namespace std;

void solve()
{
	ll int n,q;
	cin>>n>>q;
	string s;
	cin>>s;

	vi v(n);
	if(s[0]=='1') v[0]=1;
	for(ll int i=1;i<n;i++)
	{
		if(s[i]=='1')
		{
			v[i]=v[i-1]+1;
		}
		else
		{
			v[i]=v[i-1];
		}
	}

	vector< pair<ll int,ll int> > vq;
	ll int a,b;
	ll dp[n+1];
	dp[0]=1;
	rep(i,1,n+1)
	{
		dp[i]=dp[i-1]%hell*2;
	}

	for(ll int i=0;i<q;i++)
	{
		cin>>a>>b;
		a=a-1;
		b=b-1;
		vq.pb({a,b});
	//	cout<<vq[i].first<<" "<<vq[i].second<<endl;
	}
	ll int k=0;
	while(k<q)
	{
		ll int x=vq[k].first;
		ll int y=vq[k].second;
	//	cout<<x<<" x "<<y<<endl;
		string ss;
		ll int co;
		ll int cz;
		if(x==0)
		{
			co=v[y];
			cz=y+1-co;
		}
		else
		{
			co=v[y]-v[x-1];
			cz=y-x+1-co;
		}
	//	cout<<co<<" "<<cz<<endl;
		ll int cc=1;
		ll int g=co;

		cc=dp[co-1]%hell;;

		ll int ans=(cc%hell*2-1)%hell;

		cc=(cc%hell*2-1)%hell;
		ans=(ans%hell+cc%hell*cz%hell)%hell;
		if(ans<0)
		{
			ans=ans+hell;
		}

		if(g>0)
		cout<<ans<<endl;
		else
		cout<<0<<endl;
		k++;

	}

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
using namespace std;

void solve()
{
	ll int n,q;
	cin>>n>>q;
	string s;
	cin>>s;

	vi v(n);
	if(s[0]=='1') v[0]=1;
	for(ll int i=1;i<n;i++)
	{
		if(s[i]=='1')
		{
			v[i]=v[i-1]+1;
		}
		else
		{
			v[i]=v[i-1];
		}
	}

	vector< pair<ll int,ll int> > vq;
	ll int a,b;
	ll dp[n+1];
	dp[0]=1;
	rep(i,1,n+1)
	{
		dp[i]=dp[i-1]%hell*2;
	}

	for(ll int i=0;i<q;i++)
	{
		cin>>a>>b;
		a=a-1;
		b=b-1;
		vq.pb({a,b});
	//	cout<<vq[i].first<<" "<<vq[i].second<<endl;
	}
	ll int k=0;
	while(k<q)
	{
		ll int x=vq[k].first;
		ll int y=vq[k].second;
	//	cout<<x<<" x "<<y<<endl;
		string ss;
		ll int co;
		ll int cz;
		if(x==0)
		{
			co=v[y];
			cz=y+1-co;
		}
		else
		{
			co=v[y]-v[x-1];
			cz=y-x+1-co;
		}
	//	cout<<co<<" "<<cz<<endl;
		ll int cc=1;
		ll int g=co;

		cc=dp[co-1];

		ll int ans=(cc%hell*2-1)%hell;

		cc=cc*2-1;
		cc=(cc%hell*(dp[cz]-1))%hell;
		ans=(ans%hell+cc%hell)%hell;
		if(ans<0)
		{
			ans=ans+hell;
		}

		if(g>0)
		cout<<ans<<endl;
		else
		cout<<0<<endl;
		k++;

	}

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
using namespace std;

void solve()
{
	ll int n,q;
	cin>>n>>q;
	string s;
	cin>>s;

	vi v(n);
	if(s[0]=='1') v[0]=1;
	for(ll int i=1;i<n;i++)
	{
		if(s[i]=='1')
		{
			v[i]=v[i-1]+1;
		}
		else
		{
			v[i]=v[i-1];
		}
	}

	vector< pair<ll int,ll int> > vq;
	ll int a,b;
	ll dp[n+1];
	dp[0]=1;
	rep(i,1,n+1)
	{
		dp[i]=dp[i-1]*2;
	}

	for(ll int i=0;i<q;i++)
	{
		cin>>a>>b;
		a=a-1;
		b=b-1;
		vq.pb({a,b});
	//	cout<<vq[i].first<<" "<<vq[i].second<<endl;
	}
	ll int k=0;
	while(k<q)
	{
		ll int x=vq[k].first;
		ll int y=vq[k].second;
	//	cout<<x<<" x "<<y<<endl;
		string ss;
		ll int co;
		ll int cz;
		if(x==0)
		{
			co=v[y];
			cz=y+1-co;
		}
		else
		{
			co=v[y]-v[x-1];
			cz=y-x+1-co;
		}
	//	cout<<co<<" "<<cz<<endl;
		ll int cc=1;
		ll int g=co;

		cc=dp[co-1];

		ll int ans=cc*2-1;

		cc=cc*2-1;
		ans=ans+cc*cz;
		if(g>0)
		cout<<ans<<endl;
		else
		cout<<0<<endl;
		k++;

	}

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
using namespace std;

void solve()
{
	ll int n,q;
	cin>>n>>q;
	string s;
	cin>>s;

	vector< pair<ll int,ll int> > vq;
	ll int a,b;

	for(ll int i=0;i<q;i++)
	{
		cin>>a>>b;
		vq.pb({a,b});
	//	cout<<vq[i].first<<" "<<vq[i].second<<endl;
	}
	ll int k=0;
	while(k<q)
	{
		ll int x=vq[k].first;
		ll int y=vq[k].second;

		string ss;
		ll int co=0;
		ll int cz=0;
		for(ll int i=x-1;i<y;i++)
		{
			ss=ss+s[i];
			if(s[i]=='1')
			co++;
			else
			cz++;
		}
		ll int cc=1;
		ll int g=co;

		while(co>1)
		{
			cc=cc*2;
			co--;
		}

		ll int ans=cc*2-1;

		cc=cc*2-1;
		ans=ans+cc*cz;
		if(g>0)
		cout<<ans<<endl;
		else
		cout<<0<<endl;
		k++;

	}

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vii         vector<pii>
#define mi          map<ll int,ll int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
using namespace std;

void solve()
{
	ll int n,q;
	cin>>n>>q;
	string s;
	cin>>s;

	vi v(n);
	if(s[0]=='1') v[0]=1;
	for(ll int i=1;i<n;i++)
	{
		if(s[i]=='1')
		{
			v[i]=v[i-1]+1;
		}
		else
		{
			v[i]=v[i-1];
		}
	}

	vector< pair<ll int,ll int> > vq;
	ll int a,b;
	ll dp[n+1];
	dp[0]=1;
	rep(i,1,n+1)
	{
		dp[i]=dp[i-1]%hell*2;
	}

	for(ll int i=0;i<q;i++)
	{
		cin>>a>>b;
		a=a-1;
		b=b-1;
		vq.pb({a,b});
	//	cout<<vq[i].first<<" "<<vq[i].second<<endl;
	}
	ll int k=0;
	while(k<q)
	{
		ll int x=vq[k].first;
		ll int y=vq[k].second;
	//	cout<<x<<" x "<<y<<endl;
		string ss;
		ll int co;
		ll int cz;
		if(x==0)
		{
			co=v[y];
			cz=y+1-co;
		}
		else
		{
			co=v[y]-v[x-1];
			cz=y-x+1-co;
		}
	//	cout<<co<<" "<<cz<<endl;
		ll int cc=1;
		ll int g=co;

		cc=dp[co-1];

		ll int ans=(cc%hell*2-1)%hell;

		cc=cc*2-1;
		ans=(ans%hell+cc%hell*cz%hell)%hell;
		if(ans<0)
		{
			ans=ans+hell;
		}

		if(g>0)
		cout<<ans<<endl;
		else
		cout<<0<<endl;
		k++;

	}

}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--)
        solve();
    }


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define m                   1000000007
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ll ans=1;
        ans+=((temp)*(temp-1));
        ans%=m;
        if(temp==y-x+1)
        {
            cout<<ans<<endl;
            continue;
        }
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        ans+=t;
        //cout<<ans<<endl;
        ans+=(t*(((y-x-temp)*(y-x-temp+1))%m))%m;
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ll ans=1;
        ans+=((temp)*(temp-1));
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        ans+=t;
        //cout<<ans<<endl;
        ans+=((t*(y-x-temp)*(y-x-temp+1)));
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ll ans=1;
        ans+=((temp)*(temp-1));
        if(temp==y-x+1)
        {
            cout<<ans<<endl;
            continue;
        }
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        ans+=t;
        //cout<<ans<<endl;
        ans+=((t*(y-x-temp)*(y-x-temp+1)));
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ll ans=1;
        ans+=((temp)*(temp-1));
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        ans+=(t*(y-x+1-temp)*(y-x+2-temp))/2;
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define m                   1000000007
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ll ans=1;
        ans+=((temp)*(temp-1));
        ans%=m;
        if(temp==y-x+1)
        {
            cout<<ans<<endl;
            continue;
        }
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        t+=m;
        t%=m;
        ans+=t;
        ans%=m;
        //cout<<ans<<endl;
        ans+=(t*(((y-x-temp)*(y-x-temp+1))%m))%m;
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define m                   1000000007
int main()
{
    fast;
    ll n,q,i,x,y,temp,j,ans;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]= {};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1; i<n; i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        for(i=1,ans=1; i<=(y-x)+1; i++)
        {
            ans*=2;
            ans%=m;
        }
        ans-=1;
        ans+=m;
        ans%=m;
        for(i=1,j=1; i<=(y-x)+1-temp; i++)
        {
            j*=2;
            j%=m;
        }
        if((y-x)+1-temp)
        {
            j-=1;
            j+=m;
            j%=m;
            ans-=j;
            ans+=m;
            ans%=m;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
ll bigmod(ll n,ll p,ll m)
{
    if(p==0) return 1;
    if(p%2==0)
    {
        ll ret = bigmod(n,p/2,m);
        return (ret*ret)%m;
    }
    else
        return ((n%m)*(bigmod(n,p-1,m)))%m;
}
#define m                   1000000007
int main()
{
    fast;
    ll n,q,i,x,y,temp,j,ans;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]= {};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1; i<n; i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        if(temp==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        ans=bigmod(2,(y-x)+1,m);
        ans-=1;
        ans+=m;
        ans%=m;
        if((y-x)+1-temp)
        {
            j=bigmod(2,(y-x)+1-temp,m);
            j-=1;
            j+=m;
            j%=m;
            ans-=j;
            ans+=m;
            ans%=m;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll                  long long int
#define llu                 long long unsigned
#define ld                  long double
#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                "\n"
#define pb                  push_back
#define bp                  pop_back
#define pf                  push_front
#define fp                  pop_front
#define pp                  pop_up
#define mp                  make_pair
#define min3(a,b,c)         min(a,min(b,c))
#define max3(a,b,c)         max(a,max(b,c))
#define min4(a,b,c,d)       min(min(a,b),min(c,d))
#define max4(a,b,c,d)       max(max(a,b),max(c,d))
#define max5(a,b,c,d,e)     max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e)     min(min3(a,b,c),min(d,e))
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
int main()
{
    fast;
    ll n,q,i,x,y,temp;
    cin>>n>>q;
    cin.ignore();
    string s;
    cin>>s;
    ll a[n]={};
    if(s[0]=='1')
    {
        a[0]=1;
    }
    for(i=1;i<n;i++)
    {
        if(s[i]=='1')
            a[i]=1;
        a[i]+=a[i-1];
    }
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        temp=a[y];
        if(x!=0)
            temp-=a[x-1];
        ll ans=1;
        ans+=((temp)*(temp-1));
        ll t=1;
        for(i=1;i<=temp;i++)
            t*=2;
        t--;
        ans+=(t*(y-x+1-temp)*(y-x+2-temp))/2;
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  for (i=1;i<=100005;i++) f[i]=(f[i-1]*2+1)%((ll)(1e9+7));
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   ll l=t[y]-t[x-1];
   cout << (f[y-x+1]-f[y-x+1-l]+(ll)1e9+7)%(ll)(1e9+7) << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  //for (i=1,j=1;i<=100000;j*=2,j%=(ll)(1e9+7),i++) f[i]=(f[i-1]+j)%(ll)(1e9+7);
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   int l=0;if (t[y]-t[x-1]>0)l=(ll)(pow(2,t[y]-t[x-1]-1)*2-1)%(1e9+7);
   int p=0;if (y-x+1>t[y]-t[x-1])p=(ll)(pow(2,(y-x-t[y]+t[x-1]))*2-1)%(1e9+7);
   cout << (l+(l*p))%(ll)(1e9+7) << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  for (i=1,j=1;i<=100000;j*=2,i++) f[i]=(f[i-1]+j)%(ll)(1e9+7);
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   ll l=t[y]-t[x-1];
   //if (l==0) {cout << 0 << "\n";continue;}
   cout << (f[l]+(f[y-x+1-l]*f[l])%(ll)(1e9+7))%(ll)(1e9+7) << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  //for (i=1,j=1;i<=100000;j*=2,j%=(ll)(1e9+7),i++) f[i]=(f[i-1]+j)%(ll)(1e9+7);
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   int l=0;if (t[y]-t[x-1]>0)l=(ll)((ll)pow(2,(ld)(t[y]-t[x-1]-1))%(ll)(1e9+7)*2-1)%(ll)(1e9+7);
   int p=0;if (y-x+1>t[y]-t[x-1])p=((ll)pow(2,y-x-t[y]+t[x-1])%(ll)(1e9+7)*2-1)%(ll)(1e9+7);
   cout << (l+(l*p)%(ll)(1e9+7))%(ll)(1e9+7) << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  //for (i=1,j=1;i<=100000;j*=2,j%=(ll)(1e9+7),i++) f[i]=(f[i-1]+j)%(ll)(1e9+7);
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   int l=0;if (t[y]-t[x-1]>0)l=(ll)((ll)pow(2,(t[y]-t[x-1]-1)%31)%(ll)(1e9+7)*2-1)%(ll)(1e9+7);
   int p=0;if (y-x+1>t[y]-t[x-1])p=(ll)((ll)pow(2,(y-x-t[y]+t[x-1])%31)%(ll)(1e9+7)*2-1)%(ll)(1e9+7);
   cout << (l+(l*p)%(ll)(1e9+7))%(ll)(1e9+7) << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[100001];
ll u;
vector <ll> v;

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  for (i=1,j=1;i<=100000;j*=2,i++) f[i]=f[i-1]+j;
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   ll l=t[y]-t[x-1];
   if (l==0) {cout << 0 << "\n";continue;}
   cout << f[l]+f[y-x+1-l]*f[l] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define len length()
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pu push
#define ld long double
#define pld pair<ld,ld>

using namespace std;

ll n,m,i,j,k,l=0,x,y,h=1,r,o,p=0;
string s;
map <ll,ll> f;
ll t[200002];

int main()
{
  //freopen("div7.in","r",stdin);
  //freopen("div7.out","w",stdout);
  ios_base::sync_with_stdio(0);
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> n >> m;
  cin >> s;
  //for (i=1,j=1;i<=100000;j*=2,j%=(ll)(1e9+7),i++) f[i]=(f[i-1]+j)%(ll)(1e9+7);
  for (i=0;i<n;i++)
  {
   t[i+1]=t[i]+(s[i]=='1');
  }
  for (i=0;i<m;i++)
  {
   ll x,y;
   cin >> x >> y;
   int l=0;if (t[y]-t[x-1]>0)l=(ll)(pow(2,t[y]-t[x-1]-1)*2-1)%((ll)(1e9+7));
   int p=0;if (y-x+1>t[y]-t[x-1])p=(ll)(pow(2,(y-x-t[y]+t[x-1]))*2-1)%((ll)(1e9+7));
   cout << (l+(l*p))%((ll)(1e9+7)) << "\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

const long MOD =1e9 + 7;

long long po(int n){
    long long ans=1;
    if(n ==0)
        return 1;
    if(n == 1)
        return 2;
    long long x;
    x = po(n/2)%MOD;
    ans=(x*x)%MOD;
    if(n%2)
        ans*=2%MOD;
    return ans%MOD;

}

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<long> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = po(nbr1);
        nbr0 = r-l+1 -nbr1;
        ans = tt-1;
        ans = ans*po(nbr0);
        printf("%lli\n",ans);

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long po(int n){
    long long ans=1;
    if(n ==0)
        return 1;
    if(n == 1)
        return 2;
    long long x;
    x = po(n/2)%(1000000007);
    ans=(x*x)%1000000007;
    if(n%2)
        ans*=2%1000000007;
    return ans%1000000007;

}

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<int> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = po(nbr1);
        nbr0 = -l+r+1 - nbr1;
        ans = tt-1;
        ans += nbr0%(1000000007) * ans%(1000000007)*(nbr0+1)%(1000000007)/2;
        cout<<ans<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int MOD =1000000007;

long long po(int n){
    long long ans=1;
    if(n ==0)
        return 1;
    if(n == 1)
        return 2;
    long long x;
    x = po(n/2)%MOD;
    ans=(x*x)%MOD;
    if(n%2)
        ans*=2%MOD;
    return ans%MOD;

}

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<int> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = po(nbr1);
        nbr0 = -l+r+1 - nbr1;
        ans = tt-1;
        ans += ((nbr0%MOD)*(ans%MOD)*((nbr0+1)%MOD))/2;
        cout<<ans<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<int> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = pow(2,nbr1);
        nbr0 = -l+r+1 - nbr1;
        ans = tt-1;
        ans += nbr0%(1000000007) * ans%(1000000007)*(nbr0+1)%(1000000007)/2;
        cout<<ans<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<int> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = pow(2,nbr1);
        nbr0 = -l+r+1 - nbr1;
        ans = tt-1;
        ans += nbr0*ans*(nbr0+1)/2;
        cout<<ans<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const long MOD =1e9 + 7;

long long po(int n){
    long long ans=1;
    if(n ==0)
        return 1;
    if(n == 1)
        return 2;
    long long x;
    x = po(n/2)%MOD;
    ans=(x*x)%MOD;
    if(n%2)
        ans*=2%MOD;
    return ans%MOD;

}

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<long> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = po(nbr1);
        nbr0 = r-l+1 -nbr1;
        ans = tt-1;
        ans += ((nbr0)*(tt-1)*((nbr0+1))/2)%MOD;
        printf("%lli\n",ans);

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const long MOD =1e9 + 7;

long long po(int n){
    long long ans=1;
    if(n ==0)
        return 1;
    if(n == 1)
        return 2;
    long long x;
    x = po(n/2)%MOD;
    ans=(x*x)%MOD;
    if(n%2)
        ans*=2%MOD;
    return ans%MOD;

}

int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n,q,l,r;
    string c;
    cin>>n>>q;
    cin>>c;
    vector<long> dp(n,0);
    if(c[0] =='1')
            dp[0]++;
    for(int i=1;i<n;i++){
        dp[i] = dp[i-1];
        if(c[i] =='1')
            dp[i]++;
    }
    long long nbr0,nbr1,ans,tt;
    for(int i=0;i<q;i++){
        cin>>l>>r;
        ans = 0;
        l--;
        r--;
        nbr1 = dp[r];
        if(l!=0)
            nbr1-=dp[l-1];
        tt = po(nbr1);
        nbr0 = r-l+1 -nbr1;
        ans = tt-1;
        ans = ans*po(nbr0);
        ans = ans%MOD;
        printf("%lli\n",ans);

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    ll mod = 1000000007;
    ll pows[100007],curr;pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=100006;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
    }
    int n,q;
    cin>>n>>q;
    int ones[n];

    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod) + (((power(pows[num],zeros,mod)%mod))%mod)*(zeros>0));
        cout<<ans%mod<<endl;
    }
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    ll mod = 1000000007;
    ll pows[100007],curr;pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=100006;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
        pows[i]=pows[i]%mod;
        // cout<<pows[i]<<" ";

    }
    int n,q;
    cin>>n>>q;
    int ones[n+1];
    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod) + ((pows[num]%mod)*(zeros%mod))%mod)%mod;
        cout<<ans%mod<<endl;
    }

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    ll mod = 1000000007;
    ll pows[100007],curr;pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=100006;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
    }
    int n,q;
    cin>>n>>q;
    int ones[n+1];

    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod) + (((power(pows[num],zeros,mod)%mod))%mod)*(zeros>0));
        cout<<ans%mod<<endl;
    }

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    int n,q;ll mod = 1000000007;
    cin>>n>>q;
    int ones[n];ll pows[n+1],curr;
    pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=n;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
    }
    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod) + (((power(pows[num],zeros,mod)%mod))%mod)*(zeros>0));
        cout<<ans%mod<<endl;
    }

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    ll mod = 1000000007;
    ll pows[100007],curr;pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=100006;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
    }
    int n,q;
    cin>>n>>q;
    int ones[n+1];

    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod) + ((pows[num]%mod)*(zeros%mod))%mod)%mod;
        cout<<ans%mod<<endl;
    }

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    if (x == 0)
    return 0; // In case x is divisible by p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b)
{
    if(a.fi==b.fi)
    return b.se>a.se;
    return a.fi>b.fi;
}
// make adjustment;
// void BFS()
// {
//     level[1]=1;
//     q.push(1);vis[1]=true;
//     while(!q.empty())
//     {
//         int p = q.front();
//         q.pop();
//         for(int i=0;i<adj[p].size();i++)
//         {
//             if(vis[adj[p][i]]==false)
//             {
//                 level[adj[p][i]] = level[p]+1;
//                 q.push(adj[p][i]);
//                 vis[adj[p][i]]=true;
//             }
//         }
//     }
// }
void DFS(vector<int>adj[],vector<bool>&visited,int u,int n,vector<int>&vi)
{
    visited[u]=true;
    vi.pb(u);
    for(int i=0;i<adj[u].size();i++)
    {
        if(visited[adj[u][i]]==false)
        {
            (DFS(adj,visited,adj[u][i],n,vi));
        }
    }
}

int main()
{
    FIO;
    ll mod = 1000000007;
    ll pows[100007],curr;pows[0]=0,pows[1]=1,pows[2]=3,curr=2;
    for(int i=3;i<=100006;i++)
    {
        curr = ((curr%mod)*(2%mod))%mod;
        pows[i] = ((curr%mod)+(pows[i-1]%mod))%mod;
        pows[i]=pows[i]%mod;
        // cout<<pows[i]<<" ";

    }
    int n,q;
    cin>>n>>q;
    int ones[n+1];
    string s;
    cin>>s;
    ones[0]=0;
    if(s[0]=='1')
    ones[1]=1;
    else
    ones[1]=0;
    for(int i=1;i<n;i++)
    {
        if(s[i]=='1')
        {
            ones[i+1] = ones[i]+1;
        }
        else
        {
            ones[i+1] = ones[i];
        }
    }
    for(int i=0;i<q;i++)
    {
        ll l,r,len,num,zeros,ans;
        cin>>l>>r;
        len = r-l+1;
        num = ones[r] - ones[l-1];
        zeros = len-num;
        // cout<<len<<" "<<num<<" "<<pows[num]<<endl;
        ans = ((pows[num]%mod)*(power(2,zeros,mod)))%mod;
        cout<<ans%mod<<endl;
    }

   return 0;
}


#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    long long pow2[100000];
    pow2[0]=1;
    for(int j = 1; j < 100000; j++)
            pow2[j] = (2 * pow2[j - 1]) % c;
    //cout<<"here"<<endl;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        cout << ((pow2[one] - 1) * pow2[zero]) % c << endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*73741824)%c;
        }
        t=(one+zero)%30;
        p1=(p1*t)%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*73741824)%c;
        }
        t=zero%30;
        p2=(p2*t)%c;
        cout<<(p1-p2)%c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*73741824)%c;
        }
        t=(one+zero)%30;
        //cout<<t<<endl;
        p1=(p1*(int)(pow(2,t)))%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*73741824)%c;
        }
        t=zero%30;
        //cout<<t<<endl;
        p2=(p2*(int)(pow(2,t)))%c;
        cout<<(p1-p2)%c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*73741824)%c;
        }
        t=(one+zero)%30;
        p1=(p1*t)%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*73741824)%c;
        }
        t=zero%30;
        p2=(p2*t)%c;
        cout<<(p1-p2)%c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*73741816)%c;
        }
        t=(one+zero)%30;
        //cout<<t<<endl;
        p1=(p1*(int)(pow(2,t)))%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*73741817)%c;
        }
        t=zero%30;
        //cout<<t<<endl;
        p2=(p2*(int)(pow(2,t)))%c;
        if (p1>=p2)
            cout<<(p1-p2)<<endl;
        else
            cout<<(p1-p2)+c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        long long int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*1073741824)%c;
        }
        t=(one+zero)%30;
        //cout<<t<<endl;
        p1=(p1*(int)(pow(2,t)))%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*1073741824)%c;
        }
        t=zero%30;
        //cout<<t<<endl;
        p2=(p2*(int)(pow(2,t)))%c;
        if (p1>=p2)
            cout<<(p1-p2)%c<<endl;
        else
            cout<<((p1-p2)+c)%c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1;
        for(int i=0;i<one+zero;i++)
        {
            p1=(p1*2)%c;
        }
        for(int i=0;i<zero;i++)
        {
            p2=(p2*2)%c;
        }
        cout<<p1-p2<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*73741817)%c;
        }
        t=(one+zero)%30;
        //cout<<t<<endl;
        p1=(p1*(int)(pow(2,t)))%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*73741817)%c;
        }
        t=zero%30;
        //cout<<t<<endl;
        p2=(p2*(int)(pow(2,t)))%c;
        if (p1>=p2)
            cout<<(p1-p2)<<endl;
        else
            cout<<(p1-p2)+c<<endl;
    }
}


#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        int p1=1, p2=1,t;
        for(int i=0;i<(one+zero)/30;i++)
        {
            p1=(p1*1073741824)%c;
        }
        t=(one+zero)%30;
        //cout<<t<<endl;
        p1=(p1*(int)(pow(2,t)))%c;
        for(int i=0;i<zero/30;i++)
        {
            p2=(p2*1073741824)%c;
        }
        t=zero%30;
        //cout<<t<<endl;
        p2=(p2*(int)(pow(2,t)))%c;
        if (p1>=p2)
            cout<<(p1-p2)<<endl;
        else
            cout<<(p1-p2)+c<<endl;
    }
}


#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int main()
{
    int c=1000000007;
    int n,q;
    string s;
    cin>>n>>q;
    cin>>s;
    int l=s.length();
    int z[l];
    int o[l];
    int zero=0;
    int one=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='0')
            zero++;
        else
            one++;
        z[i]=zero;
        o[i]=one;
    }
    int a,b;
    long long pow2[100005];
    pow2[0]=1;
    for(int j = 1; j < 100005; j++)
            pow2[j] = (2 * pow2[j - 1]) % c;
    //cout<<"here"<<endl;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==1)
        {
            zero=z[b-1];
            one=o[b-1];
        }
        else
        {
            zero=z[b-1]-z[a-2];
            one=o[b-1]-o[a-2];
        }
        //cout<<zero<<" "<<one<<endl;
        cout << ((pow2[one] - 1) * pow2[zero]) % c << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10;

int a,n,m,b[N],k;
vector<int>v[N];
int main(){
	cin>>n;a=1;
	for(int i=0;i<n;i++){m++;
		k=m;
		for(int j=0;j<n;j++){
			cout<<k<<" ";
			k+=2*n-a;
		}
		a+=2;
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10010;

int a[N],n,m,b[N],k;
vector<int>v[N];
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		if(i%2==0){
			for(int j=0;j<n;j++)
				v[j].push_back(k++);
		}
		else{
			for(int j=n-1;j>=0;j--)
				v[j].push_back(k++);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++)
			cout<<v[i][j]+1<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        int n;
        cin>>n;
        int i,j,k,ctr=0;
         for(i=0;i<n*n;i++){
                j=n*n;

            cout<<i+1<<" ";
            ctr++;

            cout<<j-i<<endl;
            ctr++;
        if(ctr>=n*n){
            break;
        }

         }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        int n;
        cin>>n;
        int i,j,k;
         for(i=0;i<n+n;i++){
                j=n*n;
            cout<<i+1<<" "<<j-i<<endl;
         }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        int n;
        cin>>n;
        int i,j,k;
         for(i=0;i<n;i++){
                j=n*n;
            cout<<i+1<<" "<<j-i<<endl;
         }

return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define fi firstS
#define mp make_pair

void setIO(string name = "") {
    ios_base::sync_with_stdio(0); cin.tie(0);
}

int main() {
    setIO();
    int n;
    cin >> n;
    int menor = 1,maior = n*n;
    for(int i = 0; i < n;++i){
        cout << menor << " " << maior << endl;
        menor++; maior--;
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define fi firstS
#define mp make_pair

void setIO(string name = "") {
    ios_base::sync_with_stdio(0); cin.tie(0);
}

int main() {
    setIO();
    int n;
    cin >> n;
    int menor = 1,maior = n*n;
    for(int i = 0; i < n;++i){
        for(int j = 0; j < n/2;++j){
            cout << menor << " " << maior << " ";
            menor++;maior--;
        }
        cout << endl;
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define fi firstS
#define mp make_pair

void setIO(string name = "") {
    ios_base::sync_with_stdio(0); cin.tie(0);
}

int main() {
    setIO();
    int n;
    cin >> n;
    int menor = 1,maior = n*n;
    for(int i = 0; i < n*n;++i){
        cout << menor << " " << maior << endl;
        menor++; maior--;
    }
	return 0;
}


outputstandard output
After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.

An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.

In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.

The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).

Assume that both players play optimally. Who will win the game?

Input
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).

Output
Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game.


#include <iostream>
using namespace std;
int main()
{
    int n , a, b;
    cin>>n;
    for (int i=0 ; i<n ; ++i)
    {
        cout<<i+1<<" ";
        for (int j=1 ; j<n*n ; ++j)
        {
            a=n*j;
            b=(i+j)%n;
            cout<<a+b+1<<" ";
        }
       cout<<" "<<endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n , a, b;
    cin>>n;
    for (int i=0 ; i<n ; ++i)
    {
        cout<<i+1<<" ";
        for (int j=1 ; j<n ; ++j)
        {
            a=n*j;
            b=(i+j)%n;
            cout<<a+b+1<<" ";
        }
       cout<<" "<<endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n, a, b;
    cin>>n;
    for (int i=0 ; i<n ; ++i)
    {
        cout<<i+1<<" ";
        for (int j=1 ; j<n ; ++j)
        {
            a=n*j+((j+i)%n)+1;
            cout<<a<<" ";
        }
        cout<<" "<<endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int f=1, n, f2, arr[100][100];
    cin>>n;
    f2=n*n;
    for(int i =0 ; i<n ; i++)
    {
        for(int j=0 ; j<n ; j++)
        {
            if(j%2==0)
            {
                arr[i][j]=f++;
            }
            else
            {
                arr[i][j]=f2--;
            }
        }
    }
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<n ; j++)
        {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n , a=1;
    cin>>n;
    for (int i=1 ; i<n ; ++i)
    {
        cout<<i<<" "<<n*n<<endl;
    }
    for(int j=2 ; j<n*n ; j++)
    {
        cout<<j<<" "<<n*n-a<<endl;
        a++;

    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0 ; i<n ; ++i)
    {
        cout<<i+1<<endl;
        for (int j=1 ; j<n ; ++j)
        {
            cout<<n * j + (i + j) % n + 1<<endl;
        }
        cout<<" "<<endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n , a=1;
    cin>>n;
    for (int i=1 ; i<n ; ++i)
    {
        cout<<i<<" "<<n*n<<endl;
    }
    for(int j=2 ; j<=n ; j++)
    {
        cout<<j<<" "<<n*n-a<<endl;
        a++;

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9+7)
#define ceil(x, y) (x%y==0? (x/y) : (x/y+1))
#define FOR(i, N) for(int i = 0; i < N; ++i)
#define FOR1(i, N) for(int i = 1; i <= N; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T=1;
	//cin >> T;
	FOR1(i, T)
	{
		int n; cin >> n;
		FOR1(i, n){
			cout << i << " " << n*n-i+1 << '\n';
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9+7)
#define ceil(x, y) (x%y==0? (x/y) : (x/y+1))
#define FOR(i, N) for(int i = 0; i < N; ++i)
#define FOR1(i, N) for(int i = 1; i <= N; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T=1;
	//cin >> T;
	FOR1(i, T)
	{
		int n; cin >> n;
		FOR1(i, n){
			cout << i << " " << n*n-i+1;
		}
		cout<<'\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9+7)
#define ceil(x, y) (x%y==0? (x/y) : (x/y+1))
#define FOR(i, N) for(int i = 0; i < N; ++i)
#define FOR1(i, N) for(int i = 1; i <= N; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T=1;
	//cin >> T;
	FOR1(i, T)
	{
		int n; cin >> n;
		FOR1(i, n){
			cout << i << " " << n*n-i+1 << "\n";
		}
		cout<<'\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9+7)
#define ceil(x, y) (x%y==0? (x/y) : (x/y+1))
#define FOR(i, N) for(int i = 0; i < N; ++i)
#define FOR1(i, N) for(int i = 1; i <= N; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T=1;
	//cin >> T;
	FOR1(t, T)
	{
		int n; cin >> n;
		FOR1(i, n){
			FOR(j, n/2) cout << n/2*(i-1)+1 + j << " " << n*n - (n/2*(i-1) + 1 + j) + 1 << " ";
			cout << "\n";

		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9+7)
#define ceil(x, y) (x%y==0? (x/y) : (x/y+1))
#define FOR(i, N) for(int i = 0; i < N; ++i)
#define FOR1(i, N) for(int i = 1; i <= N; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T=1;
	//cin >> T;
	FOR1(t, T)
	{
		int n; cin >> n;
		FOR1(i, n){
			FOR(j, n/2) cout << 2*i-1 + j << " " << n*n - (2*i-1+j) + 1 << " ";
			cout << "\n";

		}
	}
	return 0;
}


///-----Bismillahir Rahmanir Rahim-----///

#include<bits/stdc++.h>
using namespace std;

///---FAST I/O---///
#define Boost() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

///---DATA TYPE---///
typedef long long ll;
typedef long long int lli;
///vector
typedef vector<int> vi;
typedef vector<ll> vl;
///pair
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;

///---LOOP---///
#define rep0(i,n) for(i=0;i<n;i++)
#define rep1(i,n) for(i=1;i<=n;i++)

///---I/O---///
#define sf scanf
#define pf printf

///---OTHERS---///
#define pb push_back
#define all(c)		c.begin(),c.end()
#define MK make_pair
#define F first
#define S second
#define endl '\n'

///---INITIALIZATION---//
#define mem(a,b) memset(a, b, sizeof(a) )

///---CALCULATE---///
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

///---BIT OPERATION---///
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

///---CONSTANT---///
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

int main()
{
    Boost();

    int t;
    t=1;
    //cin>>t;
    while(t--)
    {
        int n,candyBag;
        cin>>n;
        candyBag=n*n;
        cout<<candyBag--<<endl;
        for(int i=1;i<=n-1;i++)
        {
           cout<<i<<" "<<candyBag--<<endl;
        }

    }
    return 0;
}