Problem:
Score : 900 points

Problem Statement

There is a string S of length N consisting of characters 0 and 1. You will perform the following operation for each i = 1, 2, ..., m:

Arbitrarily permute the characters within the substring of S starting at the l_i-th character from the left and extending through the r_i-th character.

Here, the sequence l_i is non-decreasing.

How many values are possible for S after the M operations, modulo 1000000007(= 10^9+7)?

Constraints

2≦N≦3000

1≦M≦3000

S consists of characters 0 and 1.

The length of S equals N.

1≦l_i < r_i≦N

l_i ≦ l_{i+1}

Input

The input is given from Standard Input in the following format:

N M
S
l_1 r_1
:
l_M r_M

Output

Print the number of the possible values for S after the M operations, modulo 1000000007.

Sample Input 1

5 2
01001
2 4
3 5

Sample Output 1

6

After the first operation, S can be one of the following three: 01001, 00101 and 00011.

After the second operation, S can be one of the following six: 01100, 01010, 01001, 00011, 00101 and 00110.

Sample Input 2

9 3
110111110
1 4
4 6
6 9

Sample Output 2

26

Sample Input 3

11 6
00101000110
2 4
2 3
4 7
5 6
6 10
10 11

Sample Output 3

143

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#include <unistd.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<ll,ll> P;
 
long long int INF = 3e18;
const ll fact_table = 800008;
double Pi = 3.1415926535897932384626;
 
//vector<ll> G[550010];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop
 
ll dx[8]={1,0,-1,0,1,1,-1,-1};
ll dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "DRUL";
//ll bit[500005];
//↓,→,↑,←
 
 
#define p(x) cout<<x<<"\n";
#define el cout<<endl;
#define pe(x) cout<<(x)<<" ";
#define ps(x) cout<<fixed<<setprecision(25)<<x<<endl;
#define pu(x) cout<<(x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define CLEAR(a) a = decltype(a)(); 
 
 
//ll mod = 998244353;
ll mod = 1000000007;
 
ll rui(ll number1,ll number2){
 
    if(number2 == 0){
        return 1;
    }else{
        ll number3 = rui(number1,number2 / 2);
        number3 *= number3;
        number3 %= mod;
        if(number2%2==1){
            number3 *= number1;
            number3 %= mod;
        }
        return number3;
    }
}
ll gcd(ll number1,ll number2){
 
    if(number1 > number2){
        swap(number1,number2);
    }
    if(number1 == 0 || number1 == number2){
        return number2;
    }else{
        return gcd(number2 % number1,number1);
    }
}
void YES(bool condition){
 
    if(condition){
        p("YES");
    }else{
        p("NO");
    }
    return;
}
void Yes(bool condition){
 
    if(condition){
        p("Yes");
    }else{
        p("No");
    }
    return;
}
 
/*
ll fact[fact_table + 5],rfact[fact_table + 5];
 
void c3_init(){
    fact[0] = rfact[0] = 1;
    for(ll i=1; i<=fact_table; i++){
        fact[i] = (fact[i-1]*i) % mod;
    }
    rfact[fact_table] = rui(fact[fact_table],mod - 2);
    for(ll i=fact_table; i>=1; i--){
       rfact[i-1] = rfact[i] * i;
       rfact[i-1] %= mod;
    }
    return;}
ll c3(ll n,ll r){
    return (((fact[n] * rfact[r]) % mod ) * rfact[n-r]) % mod;}
*/
 
ll n,m,num,sum,a,b,c,d,e,g,h,w,i,j,q,r,l;
ll k;
ll ans;
vector <ll> x;
vector <ll> y;
ll fix[500005], append[500005];
char s[500005], t[500005], u[500005];
ll dp[4004][4004];
bool ban[5005][5005];
// zero one
void bind(ll n, ll zero, ll one){

	assert(zero + one > n);
	
	for(ll i=0;i<=n;i++){
		ll j = n - i;
		if(zero < i || one < j){
			ban[i][j] = true;
		}
	}
	
	
	for(ll i=0;i<=n;i++){
		if(i + one + 1 <= n){
			ban[i][one + 1] = true;
		}
	}
	for(ll j=0;j<=n;j++){
		if(zero + 1 + j <= n){
			ban[zero + 1][j] = true;
		}
	}
	
	return;
}
int main(){
	cin >> n >> m;
	cin >> s;
	ll leftmost = INF;
	ll rightmost = -1;
	for(ll i=0;i<m;i++){
		cin >> a >> b;
		a --;
		b --;
		if(b <= rightmost)continue;
		rightmost = max(b, rightmost);
		leftmost = min(a, leftmost);
		x.pb(a);
		y.pb(b);
	}
	ll idx = leftmost;
	m = x.size();
	rightmost = x[0] - 1;
	x.pb(y.back() + 1);
	for(ll i=0;i<m;i++){
		append[i] = y[i] - rightmost;
		fix[i] = x[i+1] - x[i];
		//pe(append[i]);
		//p(fix[i]);

		rightmost = y[i];
	}
	ll zero = 0, one = 0;
	ll fixsum = 0;
	for(ll i=0;i<m;i++){
		ll myappend = append[i];
		while(myappend--){
			if(s[idx] == '1'){
				one++;
			}else{
				zero++;
			}
			idx ++;
		}
		fixsum += fix[i];
		bind(fixsum, zero, one);
	}
	one = 0, zero = 0;
	for(ll i=leftmost;i<=rightmost;i++){
		if(s[i] == '1'){
			one ++;
		}else{
			zero ++;
		}
	}
	for(ll i=0;i<=zero;i++){
		for(ll j=0;j<=one;j++){
			if(ban[i][j]){
				dp[i][j] = 0;
				continue;
			}else if(i == 0 && j == 0){
				dp[i][j] = 1;
			}else if (i == 0){
				dp[i][j] = dp[i][j-1];
			}else if (j == 0){
				dp[i][j] = dp[i-1][j];
			}else{
				dp[i][j] = dp[i-1][j] + dp[i][j-1];
				dp[i][j] %= mod;
			}
		}
	}
	//p(rightmost);
	//pe(leftmost);p(rightmost);
	
	//pe(one);p(zero);
	p(dp[zero][one]);
	/*
	for(ll i=0;i<=n;i++){
		for(ll j=0;j<=n;j++){
			pe(ban[i][j]);
		}
		el;
	}
	for(ll i=0;i<=n;i++){
		for(ll j=0;j<=n;j++){
			pe(dp[i][j]);
		}
		el;
	}
	*/
	



    return 0;
}