Problem:
Score : 1100 points

Problem Statement

There are N white balls arranged in a row, numbered 1,2,..,N from left to right.
AtCoDeer the deer is thinking of painting some of these balls red and blue, while leaving some of them white.

You are given a string s of length K.
AtCoDeer performs the following operation for each i from 1 through K in order:

The i-th operation: Choose a contiguous segment of balls (possibly empty), and paint these balls red if the i-th character in s is r; paint them blue if the character is b.

Here, if a ball which is already painted is again painted, the color of the ball will be overwritten.
However, due to the properties of dyes, it is not possible to paint a white, unpainted ball directly in blue.
That is, when the i-th character in s is b, the chosen segment must not contain a white ball.

After all the operations, how many different sequences of colors of the balls are possible?
Since the count can be large, find it modulo 10^9+7.

Constraints

1 ≤ N ≤ 70

1 ≤ K ≤ 70

|s| = K

s consists of r and b.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K
s

Output

Print the number of the different possible sequences of colors of the balls after all the operations, modulo 10^9+7.

Sample Input 1

2 2
rb

Sample Output 1

9

There are nine possible sequences of colors of the balls, as follows:

ww, wr, rw, rr, wb, bw, bb, rb, br.

Here, r represents red, b represents blue and wrepresents white.

Sample Input 2

5 2
br

Sample Output 2

16

Since we cannot directly paint white balls in blue, we can only choose an empty segment in the first operation.

Sample Input 3

7 4
rbrb

Sample Output 3

1569

Sample Input 4

70 70
bbrbrrbbrrbbbbrbbrbrrbbrrbbrbrrbrbrbbbbrbbrbrrbbrrbbbbrbbrbrrbbrrbbbbr

Sample Output 4

841634130

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    Fact F;
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = re.i();
        int K = re.i();
        string S = re.s();
        List<List<int>>[,] Div = new List<List<int>>[K+1,K+1];
        Div[0,0] = new List<List<int>>();
        Div[0,0].Add(new List<int>());
        for(int i=0;i<=K;i++){
            for(int j=0;j<=K;j++){
                Div[i,j] = new List<List<int>>();
            }
        }
        Div[0,0].Add(new List<int>());
        for(int i=1;i<=K;i++){
            for(int j=1;j<=K;j++){
                for(int k=0;k<Div[i-1,j-1].Count;k++){
                    List<int> L = new List<int>();
                    List<int> cL = Div[i-1,j-1][k];
                    L.Add(1);
                    for(int l=0;l<cL.Count;l++){
                        L.Add(cL[l]);
                    }
                    Div[i,j].Add(L);
                }
                if(j >= i){
                    for(int k=0;k<Div[i,j-i].Count;k++){
                        List<int> L = new List<int>();
                        List<int> cL = Div[i,j-i][k];
                        for(int l=0;l<cL.Count;l++){
                            L.Add(cL[l]+1);
                        }
                        Div[i,j].Add(L);
                    }
                }
            }
        }
        long count = 0;
        F = new Fact(1000);
        for(int i=0;i<=K;i++){
            for(int j=0;j<=K;j++){
                for(int k=0;k<Div[i,j].Count;k++){
                    if(Possible(Div[i,j][k],S)){
                        count += Querry(Div[i,j][k],N);
                    }
                }
            }
        }
        count %= Define.mod;
        sb.Append(count+"\n");
    }
    long Querry(List<int> L,int N){
        if(L.Count == 0){
            return 1;
        }
        int a = 0;
        int b = 0;
        for(int i=0;i<L.Count;i++){
            if(L[i] == 1){
                a += 1;
            }
            else{
                a += L[i]*2-3;
                b += 2;
            }
        }
        a += L.Count-1;
        b += 2;
        if(N < a){
            return 0;
        }
        long ans = F.GetConv(N+b-1,a+b-1);
        ans *= F.f[L.Count];
        ans %= Define.mod;
        int c = 1;
        for(int i=1;i<L.Count;i++){
            if(L[i] == L[i-1]){
                c++;
            }
            else{
                ans *= F.rf[c];
                ans %= Define.mod;
                c = 1;
            }
        }
        ans *= F.rf[c];
        ans %= Define.mod;
        return ans;
    }
    bool Possible(List<int> L,string S){
        int[] A = new int[L.Count];
        int Zero = L.Count-1;
        int One = L.Count-1;
        int Two = L.Count-1;
        for(int i=0;i<S.Length;i++){
            if(S[i] == 'r'){
                if(Zero != -1){
                    A[Zero]++;
                    Zero--;
                }
                else{
                    if(Two != -1 && L[Two] > 2 && A[Two] >= 2){
                        A[Two]++;
                        if(A[Two] == L[Two]){
                            Two--;
                        }
                    }
                }
            }
            else{
                if(One != -1 && L[One] != 1 && A[One] == 1){
                    A[One]++;
                    One--;
                }
                else{
                    if(Two != -1 && L[Two] > 2 && A[Two] >= 2){
                        A[Two]++;
                        if(A[Two] == L[Two]){
                            Two--;
                        }
                    }
                }
            }
        }
        return Zero == -1 && (One == -1 || L[One] == 1) && (Two == -1 || L[Two] <= 2);
    }
}
class Fact{
    public long[] f;
    public long[] rf;
    public Fact(int N){
        f = new long[N+1];
        rf = new long[N+1];
        for(int i=0;i<N+1;i++){
            if(i == 0){
                f[i] = 1;
            }
            else{
                f[i] = (f[i-1]*i)%Define.mod;
            }
        }
        for(int i=N;i>=0;i--){
            if(i == N){
                rf[i] = pow(f[N],Define.mod-2);
            }
            else{
                rf[i] = rf[i+1]*(i+1)%Define.mod;
            }
        }
    }
    public long pow(long N,long K){
        if(K == 0){
            return 1;
        }
        else if(K % 2 == 0){
            long t = pow(N,K/2);
            return t*t%Define.mod;
        }
        else{
            return N*pow(N,K-1)%Define.mod;
        }
    }
    public long GetFact(int N){
        return f[N];
    }
    public long GetPerm(int N,int R){
        return f[N] * rf[N-R] % Define.mod;
    }
    public long GetConv(int N,int R){
        return ((f[N]*rf[R])%Define.mod*rf[N-R])%Define.mod;
    }
    public long GetRev(int N){
        if(N == 0){
            return 1;
        }
        else{
            return rf[N] * f[N-1] % Define.mod;
        }
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}