If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1.
if ( p == 1 and q == 0 and r == 0 and last == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT