Subtraction is not associative. For example (5−2)−1=2, but 5−(2−1)=4, so (5−2)−1=5−(2−1). This means the value of an expression such as 5−2−1 depends on the order in which the subtractions are carried out. When no brackets are written, the operations are performed from left to right, so 5−2−1 means (5−2)−1.
You are given an expression of the form
x1±x2±⋯±xn,
where each ± is either + (plus) or − (minus), and x1,x2,…,xn are pairwise distinct variables.
Into the all-minus expression
x1−x2−⋯−xn
you want to insert brackets so that the result is equivalent to the given expression. For example, to obtain an expression equivalent to
x1−x2−x3+x4+x5−x6+x7,
you may bracket x1−x2−x3−x4−x5−x6−x7 as
(((x1−x2)−((x3−x4)−x5))−(x6−x7)).
We consider only fully and correctly bracketed expressions. An expression is fully and correctly bracketed when it is:
Expressions with redundant brackets, such as (), (xi), or ((⋯)), are not allowed. The expression x1−(x2−x3) is not fully bracketed either, because it lacks the outermost brackets.
Write a program that reads the given expression and computes, modulo 1,000,000,000, the number of different ways to insert n−1 pairs of brackets into x1−x2−⋯−xn so that the order of the subtractions is fully determined and the resulting expression is equivalent to the given one.
The first line contains one integer n with 2≤n≤5000, the number of variables. Each of the next n−1 lines contains a single character, + or −. The character on the i-th of these lines is the operator between xi and xi+1 in the given expression.
Print a single integer: the number of different ways, modulo 1,000,000,000, to insert n−1 pairs of brackets into x1−x2−⋯−xn so that the order of the subtractions is fully determined and the resulting expression is equivalent to the given one.