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: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
#define MAXN 3010
ll MOD = 1e9+7;

ll A[MAXN];
ll N,K,a,b;
ll E[MAXN];
string S;
vpi V;
ll memo[MAXN][MAXN];
ll overlap[MAXN];
ll fact[MAXN];
ll invfact[MAXN];

ll exp(ll a, ll b){
	if (b==0)return 1;
	ll c = exp(a,b/2);
	c = (c*c)%MOD;
	if (b%2)c=(c*a)%MOD;
	return c;
}

ll inv(ll x){
	return exp(x, MOD-2);
}

ll C(ll a, ll b){
	return (fact[a] * invfact[a-b]) %MOD * invfact[b]%MOD;
}

int main(){
	cin>>N>>K>>S;
	fact[0] = 1;
	for (int i=1;i<=N;++i)fact[i] = (fact[i-1]*i)%MOD;
	for (int i=0;i<=N;++i)invfact[i] = inv(fact[i]);

	for (int i=1;i<=N;++i){
		if (S[i-1] == '1')A[i] = 1;
		A[i] += A[i-1];
	}
	for (int i=0;i<K;++i){
		cin>>a>>b;
		E[a] = max(E[a], b);
	}
	V.pb(-1,-1);
	for (int i=1;i<=N;++i)if(E[i]){
		if (SZ(V) && V.back().s >= E[i])continue;
		V.pb(i,E[i]);
	}
	for (int i=1;i<SZ(V)-1; ++i){
		overlap[i] = max(0LL,V[i].s - V[i+1].f + 1);
	}

	// for (int i=1;i<SZ(V);++i)cout<<overlap[i]<<' ';
	// cout<<'\n';

	memo[0][0] = 1;
	for (int i=1;i<SZ(V);++i){
		// cout<<"Hi\n";
		int s = V[i-1].s+1;
		int e = V[i].s;
		// cout<<"R "<<s<<' '<<e<<' '<<A[e]-A[s-1]<<'\n';
		int l = V[i].s - V[i].f + 1;
		for (int j=0;j<=overlap[i-1];++j){
			for (int k=0;k<=overlap[i];++k){
				int ones = j + A[e] - A[s-1];
				// cout<<ones<<'\n';
				int zeroes = l - ones;
				if (ones < k)continue;
				if (zeroes + k > l)continue;
				memo[i][k] += (memo[i-1][j] * C(l-overlap[i], ones-k))%MOD;
				memo[i][k]%=MOD;
			}
		}
		// for (int k=0;k<=overlap[i];++k)cout<<memo[i][k]<<' ';cout<<'\n';
	}
	ll ans =0;
	for (int k=0;k<=overlap[SZ(V) - 1];++k)ans=(ans+memo[SZ(V)-1][k])%MOD;
	cout<<ans;
}