Problem:
Score : 700 points

Problem Statement

Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.

Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?

Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:

a_x + a_{x+1} + ... + a_{y-1} = X

a_y + a_{y+1} + ... + a_{z-1} = Y

a_z + a_{z+1} + ... + a_{w-1} = Z

Since the answer can be extremely large, print the number modulo 10^9+7.

Constraints

3 ≦ N ≦ 40

1 ≦ X ≦ 5

1 ≦ Y ≦ 7

1 ≦ Z ≦ 5

Input

The input is given from Standard Input in the following format:

N X Y Z

Output

Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.

Sample Input 1

3 5 7 5

Sample Output 1

1

Here, the only sequence that contains a 5,7,5-Haiku is [5, 7, 5].

Sample Input 2

4 5 7 5

Sample Output 2

34

Sample Input 3

37 4 2 3

Sample Output 3

863912418

Sample Input 4

40 5 7 5

Sample Output 4

562805100

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::Read;

const MOD: i64 = 1_000_000_007;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let n = iter.next().unwrap().parse().unwrap();
    let x: usize = iter.next().unwrap().parse().unwrap();
    let y: usize = iter.next().unwrap().parse().unwrap();
    let z: usize = iter.next().unwrap().parse().unwrap();

    let mut dp = (0..n + 1)
        .map(|_| vec![0; 1 << 18])
        .collect::<Vec<Vec<_>>>();
    let mut next = (0..18).map(|_| vec![0; 11]).collect::<Vec<Vec<usize>>>();
    for state in 0..18 {
        for num in 1..11 {
            next[state][num] = if state < 5 {
                if num + state < x {
                    num + state
                } else if num + state == x {
                    5
                } else {
                    0
                }
            } else if state < 12 {
                let y_num = state - 5;
                if num + y_num < y {
                    num + state
                } else if num + y_num == y {
                    12
                } else {
                    0
                }
            } else if state < 17 {
                let z_num = state - 12;
                if num + z_num < z {
                    num + state
                } else if num + z_num == z {
                    17
                } else {
                    0
                }
            } else {
                state
            };
            //            dbg!(state, num, next[state][num]);
        }
    }

    let mut next_masks = (0..1 << 18).map(|_| vec![0; 11]).collect::<Vec<Vec<_>>>();
    for mask in 0..(1 << 18) {
        for num in 1..11 {
            let mut next_mask = 0;
            for j in 0..18 {
                if (mask & (1 << j)) > 0 {
                    next_mask |= 1 << next[j][num];
                }
            }
            next_masks[mask][num] = next_mask | 1;
        }
    }

    dp[0][1] = 1;
    for i in 0..n {
        for mask in 0..(1 << 18) {
            if dp[i][mask] == 0 {
                continue;
            }
            //            println!("{} {:#b}", i, mask);

            for j in 1..11 {
                let next_mask = next_masks[mask][j];
                //                if next_mask & (1 << 17) > 0 {
                //                    dbg!(i, j, dp[i][mask]);
                //                }
                dp[i + 1][next_mask] += dp[i][mask];
                dp[i + 1][next_mask] %= MOD;
            }
        }
    }

    let mut res = 0;
    for mask in (1 << 17)..(1 << 18) {
        res += dp[n][mask];
        res %= MOD;
    }
    println!("{}", res);
}