Roy and alpha-beta trees
Difficulty: Hard

Roy has taken a liking to the [Binary Search Trees](https://en.wikipedia.org/wiki/Binary_search_tree)(*BST*). He is interested in knowing the number of ways an array $A$ of $N$ integers can be arranged to form a *BST*. Thus, he tries a few combinations, and notes down the numbers at the odd levels and the numbers at the even levels. 

You're given two values, *alpha* and *beta*. Can you calculate the sum of *Liking* of all possible *BST's* that can be formed from an array of $N$ integers? *Liking* of each *BST* is defined as follows 

    (sum of numbers on even levels * alpha) - (sum of numbers on odd levels * beta)


**Note** 

+ The root element is at level $0$ ( Even )
+ The elements smaller or equal to the parent element are present in the left subtree, elements greater than or equal to the parent element are present in the right subtree.  Explained [here](https://www.hackerrank.com/contests/w4/challenges/roy-and-alpha-beta-trees/forum/questions/6070)

If the answer is no less than $10^9+9$, output the answer % $10^9+9$. 

(If the answer is less than $0$, keep adding $10^9+9$ until the value turns non negative.)

**Input Format**  
The first line of input file contains an integer, $T$, denoting the number of test cases to follow.  
Each testcase comprises of $3$ lines.  
The first line contains $N$, the number of integers.  
The second line contains two space separated integers, _alpha_ and _beta_.  
The third line contains space separated $N$ integers_, denoting the $i^{th}$ integer in array $A[i]$.  

**Output Format**  
Output $T$ lines. Each line contains the answer to its respective test case. 

**Constraints**  

$1 \le T \le 10$  
$1 \le N \le 150$  
$1 \le A[i] \le 10^9$  
$1 \le alpha,beta \le 10^{9}$  

**Sample Input**

<pre>
4
1
1 1
1
2
1 1
1 2
3
1 1
1 2 3
5
1 1
1 2 3 4 5
</pre>

**Sample Output**

<pre>
1
0
6
54
</pre>

**Explanation**

There are $4$ test cases in total. 

+ For the first test case, only $1$ BST can be formed with `1` as the root node. Hence the *Liking* / sum is $1$. 

+ For the second test case, we get 2 BSTs of the form, the *Liking* of the first tree is $1 * 1 - 2 * 1 = -1$ and $2 * 1 - 1 * 1 = 1$, this sums to $0$, hence the answer. 


<pre>
1                  2 
 \                /
  2              1
</pre>

+ For the third test case, we get $5$ BSTs. The *Liking* of each of the BST from left  to right are $2, -2, 4, 2, 0$ which sums to $6$ and hence the answer.  

<pre>
1            2                 3          3      1
 \          / \               /          /        \
  2        1   3             1          2          3
   \                          \        /          /
    3                          2      1          2
</pre>

+ Similarly, for the fourth test case, the answer is $54$.  

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER alpha
#  2. INTEGER beta
#  3. INTEGER_ARRAY a
#

def solve(alpha, beta, a):
    # Write your code here




Reference solution (Python):

    import java.io.*;
    import java.util.Arrays;
    import java.util.StringTokenizer;

    class ANKBST02_solver {
        long go(int lo, int hi, int odd) {
            if (lo > hi) {
                return 0;
            }
            if (dp[lo][hi][odd] == -1) {
                long ans = 0;
                for (int root = lo; root <= hi; root++) {
                    //consider all BST in left subtree of root
                    ans += (go(lo, root - 1, 1 -odd) * cnt[hi - root - 1 + 1]) % MOD;
                    if (ans >= MOD) ans -= MOD;
                    if (ans < 0) ans += MOD;
                    //consider all BST in right subtree
                    ans += (go(root + 1, hi, 1 -odd) * cnt[root - 1 - lo + 1]) % MOD;
                    if (ans >= MOD) ans -= MOD;
                    if (ans < 0) ans += MOD;
                    //totTrees is total number of trees considered
                    long totTrees = (cnt[hi - root] * cnt[root - lo]) % MOD;
                    //remember to add the root as many times for each tree
                    ans += (totTrees * ((mul[odd] * a[root]) % MOD)) % MOD;
                    if(ans >= MOD) ans -= MOD;
                    if (ans < 0) ans += MOD;
                }
                dp[lo][hi][odd] = ans;
            }
            return dp[lo][hi][odd];
        }

        public void solve() throws IOException {
            cnt = generateCatalan(205, MOD);
            cnt[0] = 1;
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter pw = new PrintWriter(System.out);
            int t = Integer.parseInt(br.readLine());

            while (t --> 0) {
                int n = Integer.parseInt(br.readLine());
                assert(n <= 200);
                a = new long[n] ;
                mul = new long[2];
                int i = 0;
                for (StringTokenizer tokenizer = new StringTokenizer(br.readLine()); tokenizer.hasMoreTokens(); ) {
                    String s = tokenizer.nextToken();
                    mul[i ++] = Integer.parseInt(s);
                }
                mul[1] = -mul[1];
                i = 0;
                for (StringTokenizer tokenizer = new StringTokenizer(br.readLine()); tokenizer.hasMoreTokens(); ) {
                    String s = tokenizer.nextToken();
                    a[i ++] = Integer.parseInt(s);
                }
                assert(i == n);
                Arrays.sort(a);
                dp = new long[n][n][2];
                for (int j = 0; j < n; j++) {
                    for (int k = 0; k < n; k++) {
                        for (int l = 0; l < 2; l++) {
                            dp[j][k][l] = -1;
                        }
                    }
                }
                pw.println(go(0, n - 1, 0));
            }
            pw.close();
        }

        long[] a;
        long[][][] dp;
        long[] cnt;
        long MOD = (int) (1e9 + 9);
        long[] mul;

        public static long[] generateCatalan(int n, long module) {
            long[] inv = generateReverse(n + 2, module);
            long[] Catalan = new long[n];
            Catalan[1] = 1;
            for (int i = 1; i < n - 1; i++) {
                Catalan[i + 1] = (((2 * (2 * i + 1) * inv[i + 2]) % module) * Catalan[i]) % module;
            }
            return Catalan;
        }

        public static long[] generateReverse(int upTo, long module) {
            long[] result = new long[upTo];
            if (upTo > 1)
                result[1] = 1;
            for (int i = 2; i < upTo; i++)
                result[i] = (module - module / i * result[((int) (module % i))] % module) % module;
            return result;
        }
    }

    public class Solution {
        public static void main(String[] args) throws IOException {
                new ANKBST02_solver().solve();
        }
    }