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 Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

module modulo_util
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8), allocatable :: f(:), invf(:)
contains
  subroutine init(n)
    integer, intent(in) :: n
    integer :: i
    if (allocated(f)) deallocate(f)
    if (allocated(invf)) deallocate(invf)
    allocate(f(0:n),invf(0:n))
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
    end do
    invf(n) = inv(f(n))
    do i = n, 1, -1
      invf(i-1) = mod(invf(i)*int(i,8),md)
    end do
  end
  integer(8) function perm(n,k)
    integer, intent(in) :: n, k
    perm = 0_8
    if (k > n .or. n < 0 .or. k < 0) return
    perm = mod(f(n)*invf(n-k),md)
  end
  integer(8) function comb(n,k)
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
  end
  integer(8) function homo(n,k)
    integer, intent(in) :: n, k
    homo = 1_8
    if (n == 0 .and. k == 0) return
    homo = comb(n+k-1,k)
  end
  function inv(n) result(y)
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0_8
    y = 1_8
    do while (b /= 0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = mod(mod(y,md)+md,md)
  end
  function pow(a,b) result(r)
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: i
    integer(8) :: r, p
    r = 1_8
    p = a
    do i = 0, 32
      if (btest(b,i)) r = mod(r*p,md)
      p = mod(p*p,md)
    end do
  end
end module modulo_util
program shuffling
  use modulo_util
  implicit none
  integer :: n, m, u = 0, ll, rr, i, j
  integer, dimension(0:3000) :: l = 0, r = 0, c = 0
  character(3000) :: s
  integer(8) :: ans = 1_8
  read(*,*) n, m
  call init(n)
  read(*,*) s
  do i = 1, n
    c(i) = c(i-1)+ichar(s(i:i))-48
  end do
  do i = 1, m
    read(*,*) ll, rr
    if (rr <= r(u)) cycle
    u = u+1
    l(u) = ll
    r(u) = rr
  end do
  i = 1
  do while (i <= u)
    j = i
    do while (j < u .and. l(j+1) <= r(j))
      j = j+1
    end do
    ans = mod(ans*solve(l(i:j),r(i:j),1,0),md)
    i = j+1
  end do
  write(*,'(i0)') ans+1_8
contains
  recursive function solve(l,r,i,d) result(ret)
    implicit none
    integer, intent(in) :: l(:), r(:), i, d
    integer :: j, x
    integer(8) :: ret
    if (i == 1) then
      x = c(r(i))-c(l(i)-1)
    else
      x = d+c(r(i))-c(r(i-1))
    end if
    if (i == size(l)) then
      ret = comb(r(i)-l(i)+1,x)
      return
    end if
    ret = 0_8
    do j = max(0,x-(r(i+1)-l(i)+1)), min(x,r(i)-l(i+1)+1)
      ret = ret+mod(solve(l,r,i+1,j)*comb(l(i+1)-l(i),x-j),md)
    end do
    ret = mod(ret,md)
  end
end program shuffling