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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.*;
import java.util.*;

public class Main {

	static int[][] memo;
	static int[] stages;
	static int MOD = (int) 1e9 + 7;

	static int dp(int idx, int msk) {
		if (check(msk))
			return 0;
		if (idx == 0)
			return 1;

		if (memo[idx][msk] != -1)
			return memo[idx][msk];
		int ans = 0;

		for (int d = 1; d <= 10; d++) {
			int newMsk = updMsk(msk, d);
			ans += dp(idx - 1, newMsk);
			if(ans>=MOD)
				ans-=MOD;
		}

		return memo[idx][msk] = (int) ans;
	}

	private static int updMsk(int msk, int d) {
//		String s = numToString(d);
//		String curr = Integer.toBinaryString(msk);
//		int take = Math.min(17 - s.length(), curr.length());
//		StringBuilder ans = new StringBuilder();
//		for (int i = curr.length() - take; i < curr.length(); i++)
//			ans.append(curr.charAt(i));
//		ans.append(s);
//		int ret = 0;
//		for (int i = ans.length() - 1, bit = 0; i >= 0; i--, bit++)
//			if (ans.charAt(i) == '1')
//				ret |= 1 << bit;
//		return ret;
		return (msk<<d | (1<<(d-1))) & ((1<<17)-1); 

	}

//	static String numToString(int x) {
//		StringBuilder sb = new StringBuilder("1");
//		while (sb.length() < x)
//			sb.append("0");
//		return sb.toString();
//	}

	private static boolean check(int msk) {
		char[] a = Integer.toBinaryString(msk).toCharArray();
		ArrayList<Integer> values = new ArrayList();
		for (int i = a.length - 1, len = 0; i >= 0; i--) {
			len++;
			if (a[i] == '1') {
				values.add(len);
				len = 0;
			}
		}
		int sum = 0, stage = 2;
		for (int x : values) {
			sum += x;
			if (sum > stages[stage])
				return false;
			if (sum == stages[stage]) {
				stage--;
				sum = 0;
			}
			if (stage == -1)
				return true;
		}
		return false;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		stages = new int[3];
		memo=new int [n+1][1<<17];
		for(int []x:memo)
			Arrays.fill(x, -1);
		for (int i = 0; i < 3; i++)
			stages[i] = sc.nextInt();
		long ans=1;
		for(int i=0;i<n;i++)
			ans=ans*10%MOD;
		ans-=dp(n,0);
		if(ans<0)
			ans+=MOD;
		System.out.println(ans);
		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

		int[] nxtArr(int n) throws IOException {
			int[] ans = new int[n];
			for (int i = 0; i < n; i++)
				ans[i] = nextInt();
			return ans;
		}

	}

	static void sort(int[] a) {
		shuffle(a);
		Arrays.sort(a);
	}

	static void shuffle(int[] a) {
		int n = a.length;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int tmpIdx = rand.nextInt(n);
			int tmp = a[i];
			a[i] = a[tmpIdx];
			a[tmpIdx] = tmp;
		}
	}

}