Let's annotate each character in the route with a subscript that indicates which i.5 point it passes. That is, we denote i+1→i moves by Li, and i→i+1 moves by Ri. Then, our string must contain exactly Bi=Ai2 Li's, and Bi Ri's. In any route with minimal turns, we must have:
In addition, we note that in any route, the final Li+1 must be followed by an Li (and not an Ri+1) since otherwise Bessie would not have a way to return to 0.
We claim that this is the only restriction. That is, to count the number of paths, for each i=0,1,…,N−2 it suffices to count the number of ways to pick which Ri's that are followed by Ri+1's if Bi≥Bi+1, or which Li+1's followed by Li's if Bi≤Bi+1 (such that the last Li+1 is followed by an Li). Then, any such assignment will produce a unique valid path.
Uniqueness is clear, but to show validity, suppose we construct the route by following the assignments, where the route ends when the last L0 is reached (and all previous L0's are followed by R0's). We need to check that all of the Li's and Ri's are actually used; since the number of Ri's and Li's is the same, we need to check that this path visits Bi Li's.
We will do this by induction, where L0 is true by assumption. For the inductive step, suppose Bi Li's appear in the path. Then, if Bi≥Bi+1, then Bi−Bi+1 Li's are preceded by Ri's, and Bi+1 of them are preceded by Li+1's. So, in order for all the Li's to appear, all the Li+1's must also appear. Conversely, if Bi≤Bi+1, then since the last Li+1 is immediately followed by an Li, if not all Li+1's appear then not all Li's can appear, contradicting the inductive hypothesis. So, the constructed path contains Bi Li's for each i, and thus crosses i.5 exactly 2Bi=Ai times. Minimality (i.e. the fact that exactly (B0−1)+(∑n−2i=0|Bi−Bi+1|)+(Bn−1) turns are made) follows by the construction.
Now, if Bi≥Bi+1, then the number of assignments is just (BiBi+1). In the second the other case, since the last Li+1 must be followed by Li, the answer is (Bi+1−1Bi−1).
Thus we obtain our answer as a product of binomial coefficients:
Let T=maxiAi. We can precompute factorials in O(T) time. We can compute inverse factorials by first computing the modular inverse of T! (e.g., by raising it to MOD−2 with binary exponentiation). Then we can obtain all smaller inverse factorials in decreasing order. Now we can compute each binomial coefficient in the desired expression in O(1) time, for a total runtime of O(log(MOD)+T+N).
Python solution:
P = int(1e9+7) MAX_A = int(1e6+1) # computes a^n mod P def exp(a, n): if n == 0: return 1 base = exp((a*a)%P, n // 2) return base if n%2 == 0 else (a*base)%P # initialize facts = [1] for i in range(1, MAX_A): facts.append((facts[-1] * i)%P) inv_facts = [exp(facts[-1], P-2)] for i in range(MAX_A-1, 0, -1): inv_facts.append((inv_facts[-1] * i)%P) inv_facts.reverse() # binom(n, m) = n!/(m!(n-m)!) binom = lambda n, m : (inv_facts[m] * (facts[n] * inv_facts[n-m])%P)%P N = int(input()) A = [int(x) for x in input().split()] B = [a // 2 for a in A] ans = 1 for i in range(N-1): if B[i] >= B[i+1]: ans *= binom(B[i], B[i+1]) else: ans *= binom(B[i+1]-1, B[i]-1) ans %= P print(ans)