Symbolic Differentiation of Composed Functions Symbolic Differentiation 2: Back on the Chain Gang

Task

Write a program that takes in a certain sort of function from stdin and differentiates it with respect to x, using the chain rule.* The input function will be a string of the following form:

"trig_1 trig_2 trig_3 ... x"


where trig_n is a trigonometric function sin or cos, and there are at most four functions in the term. The example string 

"cos sin sin x"


represents the function cos(sin(sin(x))).

Your output must take the following form:

k(trig_1 trig_2 x)(trig_a trig_b trig_c x)(trig_m trig_n x) et cetera


where k is a constant and parentheses represent groups to be multiplied together.

*The chain rule states that D[f(g(x))] = f'(g(x)) * g'(x)

Examples

"sin sin sin sin x" ==&gt; 1(cos x)(cos sin x)(cos sin sin x)(cos sin sin sin x)
"cos x" ==&gt; -1(sin x)
"cos sin x" ==&gt; -1(cos x)(sin sin x)


Scoring

Your score is the length of your program in bytes, multiplied by three if you use a built-in or a library that does symbolic algebra.

Similar Challenges

The previous installment: polynomial differentiation
Polynomial integration
General differentiation
Java 8, 224 214 bytes

Let the games begin!

interface B{static void main(String[]a){String o="";int v=2,z=a.length-1,i=z,k;for(;i&gt;0;o+="x)"){if(a[--i].equals("cos")){o+="(sin ";v=(v+2)%4;}else o+="(cos ";for(k=i;++k&lt;z;)o+=a[k]+" ";}System.out.print(v-1+o);}}


This could (theoretically) extend infinitely.

Output for sin cos sin sin cos sin cos sin sin sin cos sin sin cos sin cos cos cos sin x:

1(cos x)(sin sin x)(sin cos sin x)(sin cos cos sin x)(cos cos cos cos sin x)(sin sin cos cos cos sin x)(cos cos sin cos cos cos sin x)(cos sin cos sin cos cos cos sin x)(sin sin sin cos sin cos cos cos sin x)(cos cos sin sin cos sin cos cos cos sin x)(cos sin cos sin sin cos sin cos cos cos sin x)(cos sin sin cos sin sin cos sin cos cos cos sin x)(sin sin sin sin cos sin sin cos sin cos cos cos sin x)(cos cos sin sin sin cos sin sin cos sin cos cos cos sin x)(sin sin cos sin sin sin cos sin sin cos sin cos cos cos sin x)(cos cos sin cos sin sin sin cos sin sin cos sin cos cos cos sin x)(cos sin cos sin cos sin sin sin cos sin sin cos sin cos cos cos sin x)(sin sin sin cos sin cos sin sin sin cos sin sin cos sin cos cos cos sin x)(cos cos sin sin cos sin cos sin sin sin cos sin sin cos sin cos cos cos sin x)