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: Accepted)

#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;
      
struct P{
  int x,y;
  /*bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };*/
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
long long o[3331][3333];
int l[3331];
int j[3331];

long long x,y,z,mod=1000000007;
//string r;
char r[3331];

P u[3331];
queue<int> q;
//stack<int> s;
set<int> s;
map<int,int> p;

vector<int> v[2];

bool as(P a,P b)
{
  //if(a.x!=b.x)
  //return a.x<b.x;
  return a.x<b.x;
}

long long f(int n,int m)
{
  if(n>a) return 1;
  //if(m>u[l[n]].y-n+1) return 0;
  if(o[n][m]>=0) return o[n][m];
  if(l[n]==0)
  {
   if(l[n+1]==0) return f(n+1,m);
  else
  {
    //printf("##%d %d\n",n,m);
   return f(n+1,m+j[u[l[n+1]].y]-j[n]);
  }
  }
  long long k=0;
  /*if(l[n+1]==0)
  {
    k=f(n+1,m);
  }
  else*/ if(l[n+1]!=l[n]&&l[n+1])
  {
    if(m<=u[l[n]].y-n) k=f(n+1,m+j[u[l[n+1]].y]-j[u[l[n]].y]);
    if(m&&m-1<=u[l[n]].y-n) k=(k+f(n+1,m-1+j[u[l[n+1]].y]-j[u[l[n]].y]))%mod;
  }
  else
  {
    if(m<=u[l[n]].y-n) k=f(n+1,m);
    if(m&&m-1<=u[l[n]].y-n) k=(k+f(n+1,m-1))%mod;
  }
  return o[n][m]=k;
}

int main()
{
  memset(o,-1,sizeof(o));
  scanf("%d %d",&a,&b);
  scanf("%s",r+1);
  for(int t=1;t<=a;j[t]+=j[t-1],t++)
  if(r[t]=='1') j[t]=1;
  for(int t=1;t<=b;t++)
  {
    scanf("%d %d",&n,&m);
    if(u[c].x==n) u[c].y=max(u[c].y,m);
    else if(u[c].y<m)
    {
      c++;
      u[c]={n,m};
    }
  }
  for(int t=1;t<=c;t++)
  {
    for(int i=u[t].x;i<=u[t].y;i++)
      l[i]=t;
  }
  printf("%lld",f(u[1].x,j[u[1].y]-j[u[1].x-1]));
}
//3
//21