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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

const MOD: i64 = 1e9 as i64 + 7;

fn main() {
    let (n, m): (usize, usize) = read();
    let s: Vec<u8> = read::<String>().into_bytes();
    let lr: Vec<(usize, usize)> = readn(m);
    let mut imos: Vec<usize> = vec![0; n+1];
    for i in 0..n {
        let c = if s[i] == '1' as u8 { 1 } else { 0 };
        imos[i+1] = imos[i] + c;
    }

    let mut maxto: Vec<usize> = vec![0; n];
    for &(fr, to) in &lr {
        maxto[fr-1] = max(maxto[fr-1], to);
    }

    let mut dp: Vec<Vec<i64>> = vec![vec![0; n+1]; n+1];
    dp[0][0] = 1;

    let mut prev = vec![0; n+1];
    let mut to = 0;
    for i in 0..n {
        to = max(to, i);
        let mut h = 0;
        if to < maxto[i] {
            h += imos[maxto[i]] - imos[to];
        }
        to = max(to, maxto[i]);

        for j in 0..n+1 {
            if j >= h {
                prev[j] = dp[i][j-h];
            }  else {
                prev[j] = 0;
            }
        }

        let size = to - i;
        if size == 0 {
            for j in 0..n+1 {
                dp[i+1][j] = dp[i][j];
            }
        } else {
            for j in 0..n+1 {
                let left_zero = size - j;
                let left_one = j;
                let base = prev[j];
                if base >= 1 {
                    if left_zero >= 1 {
                        dp[i+1][j] += base;
                        dp[i+1][j] %= MOD;
                    }
                    if left_one >= 1 {
                        dp[i+1][j-1] += base;
                        dp[i+1][j-1] %= MOD;
                    }
                }
            }
        }
    }

    let mut ans = 0;
    for i in 0..n+1 {
        ans += dp[n][i];
    }
    println!("{}", ans % MOD);
}