We need to count the number of distinct configurations satisfying:
- There are n days, each day has at least one event.
- The events are divided into w "good" events (distinct, e.g., labeled 1..w) and b "bad" events (distinct, e.g., labeled a,b,...? Actually letters but treat as distinct labeled b events). So total events = w + b.
- The days are partitioned into three contiguous stripes: a white stripe (good events only), then a black stripe (bad events only), then a white stripe (good events only). Each stripe length (number of days) is at least 1. So the three stripes cover all n days.
- In each stripe, all events occurring in that stripe must be of the specific type: good for white, bad for black. Each day in a stripe has at least one event. So each day consists of some number of events (>=1), all of the same type as the stripe.
- Events are ordered temporally: within a day, events occur in some order (since no simultaneous events, we consider permutations of events assigned to that day). Additionally, across days, the order of events is determined by the day order: all events on day i happen before all events on day i+1 (since days are sequential). So essentially, we are assigning each event to a specific day (subject to stripe constraints) and ordering the events within each day. However, events of same type are distinct, and we treat assignments/orderings as distinct configurations.
Thus the total number of configurations is: number of ways to assign each good event to one of the white days (two white stripes) and each bad event to one of the black days, such that each day gets at least one event, and then within each day, order the events assigned to that day (permutations). Also the days themselves are distinguishable (by position), and the stripes are determined by the lengths of the three stripes? Actually, we must also decide the lengths of the stripes: we have n days, and we need to split them into three contiguous groups with each >=1 day. So the split points: choose i, j such that first stripe days = i, second = j, third = k, with i>=1, j>=1, k>=1, i+j+k = n. So number of ways to choose i and j: (n-2) choose? Actually number of compositions of n into 3 positive parts: C(n-1,2). But careful: The stripes are also typed: first white, second black, third white. So the pattern is fixed, only the lengths vary.
Thus we need to sum over all triples (i,j,k) with i>=1, j>=1, k>=1, i+j+k=n, the number of configurations for given stripe lengths.
For fixed stripe lengths: i days of white (good only), j days of black (bad only), k days of white (good only). We have w good events and b bad events.
Now we need to distribute the w good events across the i+k white days, each white day gets at least one good event. Similarly, distribute the b bad events across the j black days, each black day gets at least one bad event. Then for each day, order the events assigned to that day (all permutations). Since events are distinct, the number of ways for white part: number of surjective functions from set of w distinct good events to the set of i+k distinct white days (ordered days; first i days of first stripe, then k days of third stripe), but with the days being distinct (they are numbered 1..n, so indeed white days have specific positions). However, careful: The two white stripes are separated by a black stripe; the days are labeled by their day number (1 to n). So the assignment of good events to specific days matters. Similarly for bad events to black days.
But after assignment, within each day we also order the events assigned to that day. For a given multiset of events assigned to a day, the number of orders = factorial of the count. So overall, we need to count assignments + internal permutations.
One way: Consider sequences of events (total ordering) such that the type pattern matches the stripe pattern, and each day has at least one event. Since days are separated by "|", and events within a day are ordered, we can think of constructing a sequence of events (total order) and then splitting into n contiguous groups (days), each group non-empty, and the type of events in each group must be all good for groups in white stripes and all bad for groups in black stripe. The groups are determined by stripe lengths.
Alternatively, we can think in terms of: For each stripe, we need to arrange the events assigned to that stripe across its days, with each day having at least one event. Within the stripe, the days are contiguous and the events are placed in order (first day's events, then second day's events, etc.), and within each day, the order of events is a permutation.
We can compute the number for given (i,j,k) as:
Number of ways to assign good events to white days: (number of ways to partition the set of w distinct items into i+k ordered nonempty subsets, and then order each subset). This is equal to: (i+k)! * S(w, i+k) * w!? Wait: The number of ways to assign each of w distinct events to one of (i+k) distinct boxes (days) with each box nonempty, and then order within each box. Typically, if we have w distinct items, and we want to distribute them into d distinct boxes with ordering within boxes, the total number is: d! * S(w, d)??? Actually, let's derive.
We have w distinct items. We want to partition them into d ordered (labeled) boxes, with each box having a permutation (order). This is equivalent to: first, choose an ordering of the items (w! permutations), then insert d-1 separators into the w-1 gaps between items to divide into d nonempty segments (since each segment corresponds to a day's events in order). However, this would assign an ordering of events and then group contiguous segments to days, with the days ordered (first segment goes to day1, second to day2, etc.). But this method ensures each day gets at least one event and the order within each day is exactly the order of that segment. That yields: number of ways to choose positions for d-1 separators among w-1 gaps: C(w-1, d-1). Then for each ordering of items: w! ways. So total = w! * C(w-1, d-1). However, this only works if we consider that days are ordered and we assign the first segment to day1, second to day2, etc. But is this equivalent to "assign each event to a day and order within day"? Yes: If we take any total ordering of the w events and then cut it into d nonempty contiguous blocks, each block gives an assignment to a day and an order within that day (the order in the block). Conversely, given an assignment of events to days (with each day nonempty) and a permutation of events within each day, we can produce a total order by concatenating the day's permutations in the day order. That mapping is bijective. Therefore, number of ways = w! * C(w-1, d-1). That is for distinct events and distinct ordered days with each day nonempty.
Alternatively, using stars and bars: Number of surjective assignments to d distinct boxes without ordering within boxes is d! * S(w,d) (Stirling numbers of second kind times permutations of boxes). But then multiply by product of factorials of box sizes to account for ordering: sum_{size compositions} (w! / (product of sizes!)) * 1? Let's check: The number of assignments with ordering is: For each distribution of sizes n1,...,nd with sum=w, each >0, the number of ways to assign which events go to each box is: choose n1 events for box1: C(w, n1) * C(w-n1, n2) ... = w! / (n1! n2! ... nd!). Then order within each box: for box i, ni! permutations => multiply by product ni! = w! / (n1!... nd!) * (n1!... nd!) = w!. Actually, that simplifies to w! for each composition? Wait, careful: After assigning which events go to each box, we then order them within each box. So for a given assignment of sets (which events in each box), the number of internal orders = product over i of (size_i)!. So the total for a given composition (n1,...,nd) is: number of ways to choose which events go to each box: multinomial coefficient w!/(n1! n2! ... nd!), times product of ni! = w!. So indeed, for each composition (n1,...,nd), the count is w!. Then number of compositions of w into d positive integers is C(w-1, d-1). So total = w! * C(w-1, d-1). That matches.
Thus for good events across d_white = i + k days: number = w! * C(w-1, i+k-1). Similarly, for bad events across j days: number = b! * C(b-1, j-1). However, careful: The white days are not all contiguous in the whole timeline? Actually, they are split into two groups separated by black days. But the ordering of events across days respects the day order: all events in first white stripe (i days) occur before any event in black stripe, which occur before events in second white stripe. The internal ordering within each stripe is as described: we treat the i days as a block and the k days as another block, but they are separate stripes. But the previous combinatorial count for good events across all white days as a single set of i+k ordered days, with events assigned and ordered, would produce sequences where the events from first white stripe days and second white stripe days are interleaved? No, because the days are ordered: day1...dayi (first white stripe), then black days (day i+1...i+j), then white days (day i+j+1...n). If we treat all white days as a single ordered list of days (first i days then the last k days) and assign good events to them, the concatenation of the day orders will place all good events from first stripe before any bad event? Actually, the full sequence of events is: first we have the events of day1 (white), then day2,..., day i (white), then day i+1 (black), ... , day i+j (black), then day i+j+1 (white), ... , day n (white). So the ordering of events across all days is determined by the day ordering. So if we separately assign good events to the i+k white days and bad events to the j black days, and then combine by interleaving the stripes as per day order, we get the full event sequence. Since good and bad events are distinct types, the full sequence is determined by: within white days we have sequences of good events; within black days we have sequences of bad events; and the overall sequence is the concatenation of these blocks in day order. So the total number of configurations is product of the numbers for white days and black days? However, careful: When we combine, are there any interactions? No, because the events are of different types, and the stripes are fixed; the order between good and bad events is forced by day order. So total = (number of ways to arrange good events across white days) * (number of ways to arrange bad events across black days). Because the two sets of events are distinct and their relative positions are fixed by day order. So for given (i,j,k), the number is:
W_white = w! * C(w-1, i+k-1)
W_black = b! * C(b-1, j-1)
Thus total for that composition = W_white * W_black.
But is that correct? Let's test with examples.
Example 1: n=3, w=2, b=1. n=3 days, pattern white-black-white. So possible stripe lengths: i=1,j=1,k=1 (only composition since 3 positive parts). Then i+k=2 white days. W_white = w! * C(w-1, 2-1) = 2! * C(1,1) = 2 * 1 = 2. W_black = b! * C(b-1,1-1) = 1! * C(0,0)=1*1=1. Product =2. That matches output 2.
Example 2: n=4, w=2, b=2. Possible stripe lengths: compositions of 4 into 3 positive parts: (1,1,2), (1,2,1), (2,1,1). So need to sum over these.
We can compute manually for each:
Case (1,1,2): i=1, j=1, k=2 => white days = i+k=3. W_white = w! * C(w-1, 3-1) = 2! * C(1,2) = 2 * 0 = 0 (since C(1,2)=0). So no ways. Actually, we need at least w >= d, i.e., number of white days cannot exceed w because each white day must have at least one good event. So with w=2, white days=3 impossible. So (1,1,2) yields 0.
Case (1,2,1): i=1,j=2,k=1 => white days = 2. W_white = 2! * C(1,1)=21=2. Black days =2, W_black = b! * C(b-1,2-1)=2! * C(1,1)=21=2. Product=4.
Case (2,1,1): i=2,j=1,k=1 => white days=3 again => 0. So total =4. Matches output 4.
Example 3: n=3, w=2, b=2. Stripe lengths: only (1,1,1). So i=1,k=1 => white days=2; W_white = 2! * C(1,1)=2; black days=1; W_black = 2! * C(1,0)=2*1=2; product=4. Matches output 4.
So formula seems correct.
Thus the answer is: sum over all triples (i,j,k) of positive integers with i+j+k=n, of [ w! * C(w-1, i+k-1) * b! * C(b-1, j-1) ].
We can factor out w! * b! (constants). So answer = w! * b! * sum_{i>=1, j>=1, k>=1, i+j+k=n} C(w-1, i+k-1) * C(b-1, j-1).
Let i+k = t, where t = total white days. Since i>=1, k>=1, i = something, k = n - j - i? Actually, given n and j, i+k = n - j. But i and k each >=1, so n - j >= 2. Also j >=1, so j ranges from 1 to n-2. For each j, i can range from 1 to n-j-1 (then k = n-j-i >=1). So for fixed j, the sum over i from 1 to n-j-1 of C(w-1, (i + k)-1) where i+k = n-j. But note i+k is constant = n-j. So C(w-1, i+k-1) = C(w-1, n-j-1). That does not depend on i. However, careful: The condition i>=1 and k>=1 imposes that n-j >=2, i.e., j <= n-2. For each such j, there are how many pairs (i,k) with i>=1, k>=1, i+k = n-j? That is exactly (n-j-1) possibilities (since i can be 1.. n-j-1). So for each j (1 <= j <= n-2), the contribution from all (i,k) combinations is (n-j-1) * C(w-1, n-j-1) * C(b-1, j-1). But is that always valid? Wait, we must check that C(w-1, i+k-1) is defined; if i+k-1 > w-1, then binomial = 0. So for contributions to be nonzero, we need i+k-1 <= w-1 => n-j-1 <= w-1 => n-j <= w => w >= n-j. Similarly, C(b-1, j-1) requires j-1 <= b-1 => j <= b. Also j>=1. So effectively sum over j from 1 to min(b, n-2) (since j <= n-2) and also require n-j <= w (i.e., j >= n-w). Also j must be such that n-j >=2, i.e., j <= n-2. And also w >=? actually w >=? w >= (n-j) but we also need white days >=2? Actually, if n-j=1, that would mean only one white day total, but that can't happen because we need two white stripes each at least 1 day, so total white days >=2. Indeed the condition i>=1, k>=1 implies n-j >= 2, automatically enforced by j <= n-2. So we only sum over j satisfying 1 <= j <= n-2, j <= b, and j >= n-w (since n-j <= w). Also note that w >= 2 (given), b >=1.
Thus answer = w! * b! * sum_{j = max(1, n-w)}^{min(b, n-2)} (n-j-1) * C(w-1, n-j-1) * C(b-1, j-1).
But double-check: The factor (n-j-1) counts the number of ways to split the white days (total n-j) into two positive parts i and k. However, are these splits considered distinct in terms of configurations? Yes, because the days are labeled: days 1..i are first white stripe, days i+1..i+j black, days i+j+1..n second white stripe. Different i values produce different day assignments (since the set of days that are black is fixed? Actually, given j, the black days are a contiguous block of length j, but its starting position can vary. For fixed j, the block of black days can start at day i+1 where i can be from 1 to n-j. But we also require that after black stripe there is at least one white day, so i <= n-j-1? Actually, if black days occupy positions from start to something, we need first white stripe >=1 and second white stripe >=1, so the black block cannot start at day 1 (because then first white stripe length 0) and cannot end at day n (because then second white stripe length 0). So the start day for black block can be from 2 to n-j? Let's define i = length of first white stripe. Then i>=1, and black block occupies days i+1,..., i+j. Then second white stripe length = n - (i+j) = k, which must be >=1, so i <= n-j-1. So i ranges from 1 to n-j-1, as we used. So there are (n-j-1) possible placements of the black block (or equivalently splits of white days into i and k). That's correct.
Thus the factor (n-j-1) accounts for the number of ways to choose i given j.
Hence the answer is as above.
But is there any additional factor due to ordering of events across the whole timeline? We already accounted via product of factorial and binomials; our derivation gave product of w! * C(w-1, d-1) for white days across d days, which inherently includes ordering of events within each day and across days. So we are good.
Now we need to compute modulo MOD = 1e9+9 (1000000009). Constraints: n, w, b up to 4000. So we can precompute factorials and binomial coefficients up to max of w, b, n (4000). We need factorial up to max(w,b) maybe, but also binomial coefficients like C(w-1, something) and C(b-1, something) with arguments up to ~4000. So we can precompute factorials up to 4000 and inverse factorials. MOD is prime? 1e9+9 is indeed prime (I think 1000000009 is a prime, often used). So we can compute modular inverses.
Thus algorithm:
-
Input n, w, b.
-
Precompute fact[0..maxN] and invFact[0..maxN] where maxN = max(w,b) maybe max(w,b, n) but binomial arguments are at most w-1, b-1, so maxN = max(w,b). Also we need factorials for w and b. So set Nmax = max(w,b). Actually n can be up to 4000 but we only need factorials up to 4000 as well. We'll compute up to 4000.
-
Compute w_fact = fact[w], b_fact = fact[b].
-
Now compute sum = 0.
-
For j from 1 to n-2 (since j>=1 and <= n-2), but also j <= b and j >= n-w (since w >= n-j). Actually we need to enforce j <= b and n - j <= w, i.e., j >= n - w. Also note that j must be at most b, but also j >=1 and j <= n-2. So we set lower = max(1, n-w), upper = min(b, n-2). If lower > upper, sum = 0.
-
For each j in that range:
- d_white = n - j
- Check d_white >=2? Already ensured by j <= n-2 => d_white >=2. But also we need w >= d_white? That is exactly n - j <= w which we enforced with j >= n-w.
- number_of_splits = (d_white - 1) = n - j - 1. Because i from 1 to d_white -1.
- term = (n - j - 1) * C(w-1, d_white - 1) * C(b-1, j-1) mod MOD.
- sum += term mod MOD.
-
answer = w_fact * b_fact % MOD * sum % MOD.
Double-check edge cases: If w < 2? But constraint w>=2, so ok. b>=1.
Test with examples:
Ex1: n=3,w=2,b=1 -> n-2=1 so j range: lower = max(1, n-w = 3-2=1) =1, upper = min(b=1,1)=1. j=1. d_white = 2, splits = n-j-1=1. C(w-1=1, d_white-1=1)=C(1,1)=1. C(b-1=0, j-1=0)=1. term=111=1. sum=1. answer = 2! 1! 1 =211=2. OK.
Ex2: n=4,w=2,b=2. n-2=2. lower = max(1, n-w=4-2=2)=2, upper = min(b=2,2)=2 -> j=2. d_white = 2, splits=4-2-1=1. C(1,1)=1, C(1,1)=1. term=1. sum=1. answer = 2!*2!*1=4. But earlier we got 4? Wait, earlier we got sum over j? Actually we predicted sum=1? But we had product of w! and b! times sum=1 gives 4. Yes, answer=4.
But earlier we also considered j=1? In example 2, we had j=2 only because j=1 would give white days =3 which is > w, so term zero. So sum=1. Good.
Ex3: n=3,w=2,b=2. n-2=1. lower = max(1, n-w=3-2=1)=1, upper = min(b=2,1)=1 -> j=1. d_white=2, splits=1. C(1,1)=1, C(1,0)=1. term=1. sum=1. answer=2!*2!*1=4. OK.
Thus formula works.
Now confirm for larger possible values: Example n=5,w=3,b=3. Let's compute manually some possibilities? But trust.
We should also consider if any day can have more than one event. Yes, as long as each day has at least one. Our combinatorial counting accounted for that via compositions.
Now we need to verify that we haven't missed any factor due to ordering of events across the whole timeline possibly mixing good and bad events? But they are separated by stripes, so the ordering is forced: all good events from first white stripe appear before any bad events, which appear before good events from second white stripe. That is automatically satisfied by the assignment of days. However, does the ordering within a day matter for distinguishing configurations? Yes, we counted that via the factorial approach. Also, could there be any additional factor from permuting the two white stripes? No, because they are distinct positions (first vs second stripe). Our splitting into i and k distinguishes them. So it's fine.
One more nuance: The problem statement says: "Even if some events occur on the same day, they go in some order (there are no simultaneous events)." So our approach counts orderings within a day. And "distinct ways this scenario can develop" includes both assignment of events to days and the order within days.
Thus answer is as derived.
But wait: Are there any constraints about "the events of the same type are distinct from each other"? Yes, we treat them as labeled (numbers and letters). So distinct permutations matter.
Thus the answer is simple.
But we need to consider modulo 1000000009. Precompute factorials up to 4000. Since 4000! is huge but we compute mod.
Potential pitfalls: The binomial coefficient C(w-1, d_white-1) where d_white-1 could be larger than w-1 -> 0. Our range restrictions already filter these out via j >= n-w. Similarly, C(b-1, j-1) is zero if j-1 > b-1, i.e., j > b, so we restrict j <= b.
Also note that w and b could be as large as 4000, n as small as 3, but w+b >= n. So it's possible that w < n? Actually w+b >= n, but w alone could be less than required white days for some splits. Our sum only includes feasible j where n-j <= w. So fine.
Now complexity: O(n) up to 4000, trivial.
Double-check with some random cases by brute force for small n, w, b to ensure formula correct.
Let's attempt to manually compute for n=3, w=3, b=1.
Possible stripe lengths: only (1,1,1). White days total =2. w=3, so we have 3 good events to place into 2 white days, each at least 1. Number of ways: w! * C(w-1, d-1) = 6 * C(2,1)=62=12. Black: b=1, j=1 => 1! * C(0,0)=1. So total 12. Let's try to list possibilities? There are many. But we can check if sum of compositions? According to formula answer = 12. But is that plausible? Let's try to count manually: 3 good events labeled 1,2,3; 1 bad event a. Days: day1 white, day2 black, day3 white. Need at least one good each white day. Distribute goods: ways to assign goods to day1 and day3 with each nonempty, and order within each day. Number of assignments: For each ordered partition of {1,2,3} into two ordered nonempty subsets (day1, day3), then order within each. As before, total = 3! * C(2,1) = 62=12. And for black day, only a, order trivial. So 12. So answer 12.
Now consider n=4, w=3, b=2.
List possible stripe length triples: (1,1,2), (1,2,1), (2,1,1). Check each:
- (1,1,2): i=1, j=1, k=2 => white days total=3. w=3, so d=3, white ways: w! * C(2,2)=61=6. black days j=1: b=2, black ways: 2! * C(1,0)=21=2. product=12. splits? Actually for this triple, the number of ways with that specific (i,j,k) is exactly the product, because i and k are fixed. But our summation counted all splits (i,k) for given j and total white days. In our sum, for j=1, d_white = n-j=3, splits = n-j-1 = 2. Then term = splits * C(w-1,d_white-1)C(b-1,j-1) = 2 * C(2,2)=21=2 * C(1,0)=1 => 2. But then multiplied by w! * b! =62=12 gives 24? Wait, careful: In our summation, we multiplied w! * b! outside. So term inside sum is (n-j-1) * C(w-1, d_white-1) * C(b-1, j-1). For j=1, term = (4-1-1=2) * C(2,2)=1 * C(1,0)=1 => 2. Then total answer = w! * b! * sum = 622 = 24. But we earlier computed for (1,1,2) product=12, and there is also (2,1,1)? Let's recalc: Actually, the sum over triples should include each distinct (i,j,k) separately, not aggregated by j only. Our aggregation grouped by j and multiplied by number of splits. That is correct because for a fixed j, each split (i) yields a distinct scenario. For n=4, j=1, possible i values: i=1,2? i must be between 1 and n-j-1 =2. So i=1 => k=2? Since i+j+k=4 => k=4-1-1=2; i=2 => k=1. So two triples: (1,1,2) and (2,1,1). Both have j=1. For each triple, product = w!C(w-1,2)? Wait, d_white = i+k = 3, so white ways = 6C(2,2)=6; black ways =2; product=12. So each triple contributes 12, total 24. And also j=2? Let's compute j=2: lower = max(1, n-w = 4-3=1) =1, upper = min(b=2, n-2=2) =2, so j=2 also included. For j=2, d_white =2, splits = n-j-1=1, term =1 * C(2,1)=2 * C(1,1)=1 =>2. Then contribution from j=2 times w!b! =622=24. But j=2 corresponds to triple (1,2,1)? That's the only split: i from 1 to n-j-1 =1, so i=1, k=1. So triple (1,2,1). For this triple, white days total=2, white ways =6 * C(2,1)=62=12; black days=2, black ways =2 * C(1,1)=21=2; product=24? Wait earlier we computed for (1,2,1): white ways = w! * C(2,1)=62=12; black ways = b! * C(1,1)=21=2; product=24? That's 24, not 12? I made a mistake earlier: For (1,2,1), white days total = i+k =2. White ways = w! * C(w-1, d-1) = 6 * C(2,1) = 6 * 2 = 12. Black days = j=2, black ways = b! * C(b-1, j-1) = 2! * C(1,1) = 2 * 1 = 2. Product = 24. Yes, I earlier incorrectly said product=4? In example 2 w=2,b=2 gave product=4; for w=3,b=2, product is larger. So for j=2, product is 24. Then total sum from j=1 (24) + j=2 (24) = 48. Wait, but our aggregated sum gave for j=2 term=2, times w!b! =62*2=24, not 48. Actually, careful: For j=2, term = (n-j-1)*C(w-1, d_white-1)C(b-1, j-1) = (4-2-1=1) * C(2,1)=2 * C(1,1)=1 =>2. Multiply by w!b! (62=12) yields 24. So from j=2 we get 24. j=1 gave term=2, times 12 = 24. Total = 48. That seems plausible: we have three triples: (1,1,2) contributes 12? No, wait we recalc (1,1,2) product? Let's recompute accurately.
For each triple (i,j,k):
- white days total = i+k.
- white ways = w! * C(w-1, (i+k)-1)
- black ways = b! * C(b-1, j-1)
So product = w! * b! * C(w-1, i+k-1) * C(b-1, j-1)
Now for (i,j,k) = (1,1,2): i+k=3, C(w-1,2) = C(2,2)=1, C(b-1,0)=C(1,0)=1. So product = w! b! * 11 = 62=12.
For (2,1,1): i+k=3, same -> product = 12.
For (1,2,1): i+k=2, C(w-1,1)=C(2,1)=2, C(b-1,1)=C(1,1)=1 -> product = 622*1=24.
Total = 12+12+24 = 48.
So indeed answer should be 48.
Now compute via our summation formula: sum over j:
j=1: term = (n-j-1) * C(w-1, n-j-1) * C(b-1, j-1) = (2) * C(2,2)=1 * C(1,0)=1 =>2.
j=2: term = (1) * C(2,1)=2 * C(1,1)=1 =>2.
Sum = 4.
Then answer = w! * b! * sum = 12 * 4 = 48. Good.
So the formula holds.
Now verify another random small case with brute force maybe in mind: n=3,w=3,b=2. Only (1,1,1). White days=2, white ways = 6 * C(2,1)=12. Black days=1, black ways = 2! * C(1,0)=2. product=24. So answer=24.
Our formula: n=3, n-2=1, lower = max(1, n-w=3-3=0) so 1, upper = min(b=2,1)=1 -> j=1. d_white=2, splits=1. term = 1 * C(2,1)=2 * C(1,0)=1 =>2. w!b! =6*2=12, answer=24. Good.
Now test n=5,w=2,b=3. Since w=2, white days total must be <=2. But total white days = n - j. So we need n - j <=2 => j >=3. Also j <= n-2=3 (since n=5 => n-2=3) and j <= b=3. So only j=3 possible. Then d_white = 5-3=2, splits = n-j-1 = 1. term = 1 * C(w-1=1, d_white-1=1) =1 * C(1,1)=1? Actually C(1,1)=1, times C(b-1=2, j-1=2) = C(2,2)=1 => term=1. w!b! = 2! * 3! = 26=12. answer=12. Let's try to manually count? White stripe total days =2, with w=2 good events. So white days must have exactly 1 good each (since total 2). So assignment of goods to the two white days: there are w! * C(w-1, d-1) = 2! * C(1,1)=21=2 ways? Actually w! * C(w-1, d-1) = 2 * 1 =2. But that's for a fixed pair of white days with known order (first white stripe days and second). However, there are splits: the two white days can be distributed as (i,k) with i>=1,k>=1 and i+k=2, so possible (1,1) only. So exactly one split. Then white ways = 2. Black days: j=3, b=3. Need to assign 3 bad events to 3 days, each at least one => exactly one per day. Number of ways = b! * C(b-1, j-1) = 6 * C(2,2)=61=6? Wait b! * C(b-1, j-1) = 6 * C(2,2)=61=6. But is that correct? Let's derive: 3 distinct bad events to 3 distinct days, each day nonempty. Since total events equals number of days, the only way is each day gets exactly one event. Number of assignments: assign which event to which day: that's 3! =6. Within each day, ordering of the single event is trivial. So total =6. So product =2*6=12. That matches answer 12. Good.
Now test n=5,w=3,b=2. White days total = n - j. j must be between? We need j >= n-w =5-3=2, j <= min(b=2, n-2=3) => j=2 only (since j <=2). Then d_white =3, splits = n-j-1 = 5-2-1=2. term = 2 * C(w-1=2, d_white-1=2) = 2C(2,2)=21=2, times C(b-1=1, j-1=1) = C(1,1)=1 => term=2. w!b! =62=12 => answer 24. Now manually: possible j=2. d_white=3. splits: i can be 1 or 2 (k=2 or 1). So two triples: (1,2,2?) Actually i + k =3, with i>=1,k>=1. So (1,2) and (2,1) meaning (i=1,k=2) and (i=2,k=1). For each triple, white ways = w! * C(2,2)=61=6. Black ways for j=2: b! * C(1,1)=2*1=2. So product per triple = 12. Two triples => 24. Good.
Now test n=5,w=3,b=3. Then j can range? n-w=2, so j>=2; b=3, n-2=3 so j <=3. So j=2,3.
- j=2: d_white=3, splits=2, C(w-1,2)=C(2,2)=1, C(b-1,1)=C(2,1)=2? Wait b-1=2, j-1=1 => C(2,1)=2. term = 2 * 1 * 2 =4.
- j=3: d_white=2, splits=1, C(w-1,1)=C(2,1)=2, C(b-1,2)=C(2,2)=1, term = 1 * 2 * 1 =2.
Sum=6. w!b! =6*6=36. answer=216.
Let's verify via manual counting maybe conceptually? Might be correct.
Now test an extreme: n=4000, w=4000, b=4000. Then our sum goes over j from max(1, n-w=0) =1 to min(b=4000, n-2=3998) => up to 3998. For each j, term = (n-j-1) * C(w-1, n-j-1) * C(b-1, j-1). That's huge but manageable with precomputed binomials. So algorithm is fine.
But we need to be careful about modulo: MOD=1000000009, a prime. We'll compute factorials up to 4000, and inverse factorials.
One more nuance: The binomial coefficient C(w-1, n-j-1) requires w-1 up to 3999. That's fine.
Now, double-check if there is any other factor that we missed: The problem statement: "What is the number of distinct ways this scenario can develop over the next n days if Polycarpus is in for a white stripe (a stripe that has good events only, the stripe's length is at least 1 day), the a black stripe (a stripe that has not-so-good events only, the stripe's length is at least 1 day) and a white stripe again (a stripe that has good events only, the stripe's length is at least 1 day). Each of n days will belong to one of the three stripes only." That matches.
Now, sample cases are as provided.
Thus we can code.
But before coding, consider if there might be any alternative interpretation: Could the order of events across days be considered as the sequence of events, and then the days are just divisions? We've captured that.
Now, potential pitfall: In the black stripe, there might be bad events only. But if b=0? Constraint b >= 1, so okay.
Thus solution: compute factorial and inv factorial arrays up to N = max(w,b) maybe up to 4000. Since we need C(w-1, something) and C(b-1, something). Also we need factorials for w and b. So precompute up to max(w,b). Also note that we may need up to w-1 and b-1, but factorial up to max(w,b) is enough. Actually, we need factorial for numbers up to max(w,b), but for binomial we need factorial up to max(w-1, b-1, maybe n?) But n can be up to 4000, and we compute C(w-1, n-j-1) where w-1 could be up to 3999, and n-j-1 could be up to n-2 which is at most 3998, so within range. So precompute up to 4000.
Implementation steps:
- Read n, w, b.
- Set MOD = 1000000009.
- Let MAX = max(w, b) maybe also need up to n? But n can be 4000, but we may need factorial of numbers up to max(w,b) which is <=4000. However, for binomial coefficient we need factorials for numbers up to max(w-1, b-1). Since max(w,b) is at most 4000, that's fine. But what about computing C(w-1, something) where w-1 might be 3999? If w=4000, w-1=3999. So we need factorial up to 3999. So MAX should be at least 4000. We'll set MAXN = 4000 (or maybe 4000+5). Safer: set MAXN = 4000 + 5.
- Precompute fact[0..MAXN] and invFact[0..MAXN].
fact[0] = 1; for i=1..MAXN: fact[i] = fact[i-1]i % MOD.
invFact[MAXN] = mod_pow(fact[MAXN], MOD-2, MOD).
for i=MAXN-1 downto 0: invFact[i] = invFact[i+1](i+1) % MOD.
- Define function nCr(n, r) that returns 0 if r<0 or r>n else fact[n]*invFact[r]%MOD * invFact[n-r]%MOD.
- Compute w_fact = fact[w], b_fact = fact[b].
- Initialize sum = 0.
- For j from 1 to n-2:
- if j > b: continue (since C(b-1, j-1)=0 if j-1 > b-1, but we can skip to avoid out-of-range; also because if j > b, nCr returns 0).
- if n - j > w: continue (since C(w-1, n-j-1) requires n-j-1 <= w-1, i.e., n-j <= w; else nCr returns 0). Actually, we can compute nCr without condition; it will return 0 if out of range (we'll handle by condition in nCr to return 0 when r > n or r < 0). So we can just loop j from 1 to n-2 and compute term = (n-j-1) * nCr(w-1, n-j-1) % MOD * nCr(b-1, j-1) % MOD, and sum = (sum + term) % MOD. Since nCr will return 0 for invalid arguments, it's fine. But we must be careful that w-1 and b-1 are non-negative (w>=2 => w-1>=1; b>=1 => b-1>=0). So it's fine.
- However, we also need to ensure n-j-1 is within [0, w-1]? nCr returns 0 if out of range, so term becomes 0. So we can simply loop j=1 to n-2 and add. That is O(n) = up to 4000. So it's fine.
- But note: n-j-1 might be negative if j = n? But j max = n-2, so n-j-1 >=1? For j = n-2, n-j-1 = 1. So it's non-negative. Minimum j=1 gives n-2, so always >=1? For n=3, j=1 => n-j-1=1, okay. So it's non-negative.
- After loop, answer = w_fact * b_fact % MOD * sum % MOD.
- Print answer.
Let's test with n=3,w=2,b=1: j loop 1 to 1: term = (3-1-1=1) * nCr(1,1)=1 * nCr(0,0)=1 =>1. sum=1. answer=211=2.
Check n=4,w=2,b=2: j from 1 to 2.
- j=1: term = (4-1-1=2)*nCr(1,2)=0 * nCr(1,0)=1 =>0.
- j=2: term = (4-2-1=1)nCr(1,1)=1 * nCr(1,1)=1 =>1. sum=1. answer=22*1=4.
Check n=4,w=2,b=2 sum=1? Wait earlier we got sum=1, answer=4. Good.
Check n=4,w=3,b=2: j=1: term=2* nCr(2,2)=1 * nCr(1,0)=1 =>2; j=2: term=1* nCr(2,1)=2 * nCr(1,1)=1 =>2; sum=4; w_factb_fact=62=12; answer=48. Good.
Now we should also consider the possibility that w! * b! * sum may overflow 64-bit during multiplication before mod? In Python it's fine; but in C++ we can do mod multiplication. But we are implementing in C++? The problem may be from Codeforces? Possibly but we can implement in C++ for speed. We'll use long long with mod.
We'll compute:
long long ans = ( (w_fact * b_fact) % MOD ) * sum % MOD;
All good.
Now, double-check that we haven't missed any factor for ordering of stripes? The days themselves are distinct. The assignment of events to days and ordering within days fully determines the timeline. The stripes are just a pattern, not an extra choice. So answer as computed is final.
Now we should consider if there is any requirement that the white stripes are "white stripes" meaning they consist of good events only, but there is no requirement that the white stripes themselves are different from each other? No, they are just part of the pattern. Our splitting into i and k distinguishes them, so that's correct.
Now, what about the possibility that the same configuration can be obtained from different assignments? Our counting is bijective, as argued.
Thus solution is correct.
Now, test with some random values using brute force for small n,w,b to verify. Let's do a mental small test: n=3, w=1, but w >=2 according to constraints (w>=2). So ok.
Now maybe test n=4,w=3,b=1. w=3,b=1. j from 1 to 2.
- j=1: term = (4-1-1=2)*nCr(2,2)=1 * nCr(0,0)=1 =>2
- j=2: term = (4-2-1=1)*nCr(2,1)=2 * nCr(0,1)=0 =>0. sum=2. w_fact=6, b_fact=1, ans=12. Let's manually count? n=4, pattern white-black-white-white? Actually black stripe length j, and white stripe lengths i,k. Need i>=1,k>=1. Cases:
j=1: i can be 1 or 2? i=1 => k=2; i=2 => k=1.
For (i=1,j=1,k=2): white days total=3, w=3, so each white day must have exactly 1 good (since total goods=3). White ways = number of ways to assign 3 goods to 3 ordered days with each day exactly one: 3! =6. Actually w! * C(w-1, d-1) = 6 * C(2,2)=6. Yes 6. Black: b=1, only one bad event, placed on the single black day: 1 way. So product =6.
For (i=2,j=1,k=1): white days total=3, same white ways =6, black ways=1, product=6.
Total =12. Good.
j=2: i=1,k=1? i must be from 1 to n-j-1 =1, so (1,2,1). White days total=2, w=3 goods to assign to 2 days, each at least 1. White ways = 6 * C(2,1)=12. Black days=2, b=1? But b=1, need each black day nonempty: impossible because total bad events=1 but need at least 2 days. So product=0. So total=12. matches.
Thus formula works.
Now, we need to implement modulo operations.
One more nuance: The binomial coefficient nCr(w-1, n-j-1) when w-1 < n-j-1 yields 0. That's fine.
We also need to consider if w-1 or b-1 might be negative? w>=2 => w-1>=1; b>=1 => b-1>=0. So safe.
Now, we need to output answer modulo 1e9+9.
Now, think about maximum values: fact[4000] mod prime is fine. Precomputation O(4000). The sum loop O(n) up to 4000. So total time O(4000).
Now, edge case: n=3, w=2, b=4000? Then j only 1, check: term = (3-1-1=1)*C(1,1)=1 * C(3999,0)=1 =>1. answer=2! * 4000! * 1 mod MOD. That's fine, we compute fact[4000] etc.
Now, we need to ensure that we compute factorials up to max(w,b) which is 4000. If w=4000, w! is huge mod. That's fine.
Now, check if any potential overflow in computing product (n-j-1)CC. n-j-1 up to 3998, C up to large mod numbers. We'll use modular multiplication.
Now, we need to code.
We'll write in C++.
Implementation details:
- const int MOD = 1000000009;
- const int MAXN = 4000; // maybe 4005
- vector fact(MAXN+1), invFact(MAXN+1);
- function long long mod_pow(long long a, long long e) { ... }
But we can also compute invFact using Fermat: invFact[i] = pow_mod(fact[i], MOD-2). But iterative method is faster: compute fact, then invFact[MAXN] = pow_mod(fact[MAXN], MOD-2), then for i=MAXN-1 downto 0: invFact[i] = invFact[i+1] * (i+1) % MOD.
Now, nCr function:
long long nCr(int n, int r) {
if (r < 0 || r > n) return 0;
return fact[n] * invFact[r] % MOD * invFact[n-r] % MOD;
}
Now, compute:
long long w_fact = fact[w];
long long b_fact = fact[b];
long long sum = 0;
for (int j = 1; j <= n-2; ++j) {
long long splits = n - j - 1;
long long term = splits % MOD;
term = term * nCr(w-1, n - j - 1) % MOD;
term = term * nCr(b-1, j - 1) % MOD;
sum = (sum + term) % MOD;
}
long long ans = w_fact * b_fact % MOD * sum % MOD;
cout << ans << endl;
Now, check with n=3: j loop j from 1 to 1. splits = 1. nCr(w-1, n-j-1=1) works. Good.
Now, verify with n=4,w=2,b=2: j=1 splits=2, nCr(1,2)=0 -> term 0. j=2 splits=1, nCr(1,1)=1, nCr(1,1)=1 => term=1. sum=1. ans=221=4.
Now, check with n=4,w=3,b=2: j=1: splits=2, nCr(2,2)=1, nCr(1,0)=1 =>2; j=2: splits=1, nCr(2,1)=2, nCr(1,1)=1 =>2; sum=4; ans=624=48.
Works.
Now, consider n=4000, w=4000, b=4000. j goes up to 3998, but we must compute nCr(3999, something) and nCr(3999, something). Our factorial array up to 4000 covers n=3999? Actually, we set MAXN = 4000, so fact[4000] exists. We need fact[3999] as well. So MAXN=4000 includes index 3999. So it's okay.
Now, note: w-1 = 3999, b-1 = 3999. We have factorial up to 4000, so fact[3999] is computed, but we need invFact[3999] as well. That's fine.
Now, check for when w or b is 0? Not possible due to constraints.
Now, one more test: n=5,w=5,b=5. Then j from 1 to 3 (since n-2=3). Lower bound from n-w = 0, so j from 1. Also j <= b=5, so j=1,2,3. Compute:
- j=1: splits=3, nCr(4,3)=4, nCr(4,0)=1 => term=12.
- j=2: splits=2, nCr(4,2)=6, nCr(4,1)=4 => term=264=48? Actually splits=5-2-1=2, term =2 * 6 * 4 =48.
- j=3: splits=1, nCr(4,1)=4, nCr(4,2)=6 => term=146=24.
Sum=12+48+24=84. w_factb_fact = 120120=14400. ans = 14400*84 = 1,209,600? Let's compute mod: but we can compute later. So seems plausible.
Now, let's try to think if there is any double-counting: The assignment of good events to white days and bad events to black days, and then splitting white days into two stripes. But is there any dependency? For a fixed total white days d and split i,k, the white days are distinct: the first i days are distinct from the last k days because they are at different positions. So counting as product of white ways across all white days is fine; it doesn't depend on i and k individually except through d. And our factor (n-j-1) counts the number of splits. So all good.
Now, just to be thorough, we can also derive using generating functions, but it's not needed.
Now, about the sample notes: In third sample, they show "1|ab|2" etc. That corresponds to w=2,b=2,n=3. In that scenario, the black stripe is a single day with both bad events; order within that day matters: "ab" vs "ba". That's counted via b! * C(b-1, j-1) with j=1 => b! * C(1,0)=2*1=2, so both orders are counted. Good.
Now, we can code.
But wait: The problem states: "even the events of the same type are distinct from each other. Even if some events occur on the same day, they go in some order (there are no simultaneous events)." That aligns with our counting.
Now, check: There's also condition "At least one event is going to take place during each day." So each day nonempty, which we enforced.
Now, we should also consider: Could there be any scenario where the stripes are not necessarily exactly three? The problem says the rule is that there is a white stripe, then a black stripe, then a white stripe again. So it must be exactly three stripes. So we are correct.
Now, potential pitfall: Could the white stripes be of length zero? No, each stripe length is at least 1.
Now, we must check constraints: w>=2, b>=1, n>=3, w+b>=n. So all inputs are valid.
Now, code.
But wait: The binomial coefficient C(w-1, n-j-1) might require n-j-1 to be non-negative; for j up to n-2, it's at least 1? Actually if n=3 and j=1, n-j-1=1; if n=4,j=2 => 1; if n=4,j=3? But j max is n-2, so n-j-1 >=1. So okay. But what about n=3,j=1 gives 1. So no zero? Could n-j-1 be zero? For n=3, j=2? Not allowed because n-2=1, j max=1. So never zero. For n=4, j=3? n-2=2, so j max=2. So never zero. Actually is there any case where n-j-1 = 0? That would require j = n-1. But j max is n-2, so n-j-1 >=1. So splits always >=1. So term (n-j-1) is always at least 1. But that's fine.
Now, check for extreme: n=3, only j=1, splits=1.
Now, final answer is sum times w! b!.
Now, note: In our derivation we used w! * C(w-1, d-1) for white ways. But that counts assignments to d white days with ordering. However, note that the two white stripes are separated by a black stripe. In the overall timeline, the ordering of good events from the first white stripe and the second white stripe is determined by the day order, which we accounted for by concatenating. This is consistent with the product approach.
Now, verify again the mapping: For a fixed triple (i,j,k), we assign good events to i+k distinct days, each day nonempty, and order within each day. That gives a sequence of good events in order: day1 events (first white stripe) in some order, then day2 events (if i>1), ..., day i events, then after black stripe, day i+j+1 events, ..., day n events. So it's a concatenation of permutations for each white day. The total ordering of good events across all white days is not arbitrary; it's constrained to be a concatenation of permutations for each day. Similarly for bad events.
Our counting via w! * C(w-1, d-1) indeed counts exactly that: For any ordering of the w good events (w! permutations), we cut it into d nonempty segments; each segment corresponds to a day's events in order. That's a bijection. So for a given d, number of ways to assign good events to d ordered days with ordering within each day = w! * C(w-1, d-1). That's correct.
Now, the overall sequence of all events (good and bad) is: first the segments for the first i white days (good events), then the j black days (bad events), then the last k white days (good events). So it's a concatenation of those sequences. So the total number is product of counts for each stripe (since choices are independent). However, is there any interaction about ordering between good and bad events? No, because they are separate sequences placed in fixed positions. So product is correct.
Thus solution stands.
Now, final answer.
But wait: Could there be any alternative interpretation where the stripes themselves may be not predetermined? The problem says "Polycarpus is sure that this rule is going to fulfill during the next n days." So the pattern must hold exactly. So it's fine.
Now, consider if w or b is less than number of days in their respective stripes. Our sum automatically zeroes out those j where C's become zero.
Now, we must ensure that we use modulo 1000000009, not 1000000007.
Now, code. We'll implement in C++.
But maybe we need to use long long, and compute factorials with mod.
Implementation steps in code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000009;
const int MAXN = 4000; // Actually might need 4000 but we add buffer.
ll fact[4005], invFact[4005];
ll mod_pow(ll a, ll e) {
ll res = 1;
while (e) {
if (e & 1) res = res * a % MOD;
a = a * a % MOD;
e >>= 1;
}
return res;
}
void precompute(int maxn) {
fact[0] = 1;
for (int i = 1; i <= maxn; ++i) fact[i] = fact[i-1] * i % MOD;
invFact[maxn] = mod_pow(fact[maxn], MOD-2);
for (int i = maxn-1; i >= 0; --i) invFact[i] = invFact[i+1] * (i+1) % MOD;
}
ll nCr(int n, int r) {
if (r < 0 || r > n) return 0;
return fact[n] * invFact[r] % MOD * invFact[n-r] % MOD;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, w, b;
cin >> n >> w >> b;
int maxn = max(w, b);
precompute(maxn); // But we might need up to max(w-1, b-1) which is <= maxn, but also we might need fact[w] and fact[b], which are up to maxn. However, we also need nCr(w-1, ...) where w-1 might be maxn-1, so okay.
// But what about nCr when n is larger than maxn? We never use n larger than max(w,b) because arguments are w-1 or b-1. So maxn is enough.
// However, we might need factorial up to maybe 4000. If w and b are 4000, maxn=4000, we need fact[4000] and invFact[4000], and also for nCr(w-1,...) we need fact[3999] etc, which are within range.
// But we also need to access fact[w] for w! and fact[b] for b!. So precompute up to max(w,b).
// But caution: we also need nCr for arguments up to maybe 3999, but our array size is up to maxn=4000, which includes 3999. Good.
ll ans = 0;
ll sum = 0;
for (int j = 1; j <= n-2; ++j) {
ll splits = n - j - 1;
ll term = splits % MOD;
term = term * nCr(w-1, n-j-1) % MOD;
term = term * nCr(b-1, j-1) % MOD;
sum = (sum + term) % MOD;
}
ans = fact[w] * fact[b] % MOD * sum % MOD;
cout << ans << '\n';
return 0;
}
But we must ensure that when we call nCr(w-1, n-j-1), w-1 might be negative? w>=2 so w-1>=1. So okay. b-1 might be zero when b=1, then nCr(0, j-1) is zero unless j-1=0. That's fine.
Also, precompute up to maxn = max(w,b) but we also need factorial up to max(w,b) only. However, we also use nCr with n=w-1, which is up to max(w,b)-1. That's fine because we have fact and invFact for indices up to maxn, which covers w-1 because w-1 <= maxn-1? Actually if w = maxn, then w-1 = maxn-1, which is within range because we have fact up to maxn, and we need fact[w-1] = fact[maxn-1], which we computed. Also we need invFact for that index, which we have. So okay.
But what about nCr with n=b-1, similar.
Now, test with w=4000, b=4000: maxn=4000, we compute fact[0..4000] and invFact[0..4000]. Then we need nCr(3999, something) -> uses fact[3999], which exists. So fine.
But one more check: nCr(n,r) uses fact[n] where n can be up to maxn (4000). But if w-1 = 3999, fact[3999] is defined. So we need fact up to 3999, and invFact up to 3999 as well. Since we computed up to 4000, it's fine.
Now, what about when w=2, then w-1=1, we need fact[1], fine.
Now, ensure that the loop j from 1 to n-2 does not cause issues when n-2 could be negative? n>=3, so n-2>=1. So loop will have at least one iteration.
Now, confirm with sample input.
We can test the code mentally.
Now, check for potential performance: O(4000) trivial.
Thus answer.
But I recall there might be a nuance: The problem might also consider that the events themselves are distinct, but the order of days within each stripe may not be distinguished? No, days are distinguished by their time index. So it's fine.
Now, before finalizing, double-check with another small case manually using brute force enumeration in mind to confirm formula.
Take n=3, w=2, b=2. We already did.
Take n=3, w=2, b=3. Then only (1,1,1). White days=2, white ways=2; black days=1, b=3: black ways = b! * C(b-1,0) = 6 * 1 =6; product=12. So answer 12. Let's see if that makes sense: Good events: two distinct numbers, say 1,2. Bad events: three distinct letters a,b,c. Day1 white, day2 black, day3 white. Need at least one good each white day: assign goods to day1 and day3, each nonempty. Since total goods=2, exactly one each. So assignment of which good goes to day1: 2 choices (1 to day1,2 to day3, or vice versa). Within each day, ordering is trivial (since one event). So 2 ways for goods. For black day: we need to place all three bad events on day2, order matters: 3! = 6. So total 12. So correct.
Now, does our formula give that? n=3 => j only 1. splits = 1, term = 1 * nCr(1,1)=1 * nCr(2,0)=1 =>1. sum=1. w_fact=2, b_fact=6, ans=12. Yes.
Now, test n=4, w=4, b=1. Then j from 1 to 2. w=4, b=1.
- j=1: splits= (4-1-1=2) * nCr(3, 4-1-1? n-j-1=2) => C(3,2)=3 * nCr(0,0)=1 => term=2*3=6.
- j=2: splits= (4-2-1=1) * nCr(3, 4-2-1=1) => C(3,1)=3 * nCr(0,1)=0 => term=0.
Sum=6. w_fact=24, b_fact=1, ans=144.
Now manually? Let's enumerate possibilities: n=4, pattern white-black-white-white? Actually, black stripe length j. Possibilities: j=1 (i,k) splits: i=1,k=2 and i=2,k=1; j=2 (i,k) only i=1,k=1 (since n-j-1=1). But j=2 impossible because b=1 cannot fill 2 black days each with at least one event. So only j=1 with i=1,k=2 or i=2,k=1.
For each such triple, white days total = i+k =3. With w=4 good events to distribute to 3 white days, each nonempty. Number of ways: w! * C(3,2)=24 * C(3,2)=24*3=72? Wait C(w-1, d-1) = C(3,2)=3. So white ways = 24 * 3 = 72. Black ways: b=1, j=1 => 1. So product per triple =72. Two triples =>144. So matches.
Now, test n=5, w=4, b=2. Compute manually? Let's trust.
Now, consider potential overflow in C(0,0) etc. fine.
Now, note: The binomial coefficient function should handle r=0 returns 1. Our nCr with condition r<0? For j=1, b=1 => b-1=0, j-1=0 => r=0, returns fact[0]*invFact[0]*invFact[0] = 1. Good.
Now, also consider that w-1 could be 0? w>=2 => w-1>=1, so no. b-1 could be 0 (b=1). Then nCr(0,0)=1, and nCr(0, anything >0)=0. So that works.
Now, now check for input like n=4000, w=4000, b=1. Then j must be such that b>=j => j<=1. Also n-j <= w => j >= n-w = 0, so j=1 is allowed (j=1 <= n-2=3998? Actually j can be 1 only, but loop goes up to 3998. But for j>1, nCr(b-1, j-1)=0 because b-1=0, j-1>=1 => 0. So term will be zero. So sum will be term for j=1 only. So we can loop all j, it's fine. But we need to ensure that we don't attempt to access nCr with n=w-1, r=n-j-1 where n-j-1 may be > w-1 for large j, but that returns 0. So it's fine.
Now, consider n=4000, w=2000, b=2000. Then loop j from 1 to 3998, but many terms zero when j > b or n-j > w. So it's still fine.
Now, the precomputation of factorials up to max(w,b) works.
But one nuance: In the loop, we use nCr(w-1, n-j-1). The argument n-j-1 could be as high as n-2, up to 3998. If w-1 is less than that, it's 0. That's fine. But we need to ensure that fact and invFact arrays cover index up to max(w-1, b-1, maybe n-j-1?) Actually, nCr function uses fact[n] where n = w-1 (max 3999). But we may also need fact[n-j-1] as part of the denominator? nCr computes using fact[n], invFact[r], invFact[n-r]. So it only uses fact array at indices n, r, n-r. All these are <= max(n, r, n-r). Since n = w-1 <= 3999, r = n-j-1 could be up to 3998, but that's <= 3998, which is within maxn if maxn >= 3999. Since we set maxn = max(w,b) maybe = 2000 if w=2000, b=2000, then w-1=1999, but r could be up to 3998 > 1999, but nCr will first check if r>n, then return 0. It won't access fact[r] because it checks condition before using arrays. So it's safe: we only access fact[r] when r <= n. If r > n, we return 0 early. So we don't need fact for indices larger than w-1 or b-1. So precomputing up to max(w,b) is sufficient. However, we also need fact[w] and fact[b] which are up to max(w,b). So indeed we only need up to max(w,b). But careful: w-1 could be max(w,b)-1, but if b > w, max(w,b) = b, we need fact[w-1] which may be less than b, but we have up to b, so okay. So we set maxn = max(w,b). That's enough.
But what if w and b are both 4000, maxn=4000, we need fact[4000] and fact[3999] etc. That's okay.
Now, we should also compute fact[0]... up to maxn, inclusive.
Thus code is good.
Now, test with n=3,w=4000,b=4000: maxn=4000, precompute fact up to 4000. Loop j=1 only. n-j-1 = 1. nCr(w-1=3999, 1) =? uses fact[3999], invFact[1], invFact[3998]. Those exist because we have up to 3999 indices? Wait, fact[3999] is defined if maxn=4000 includes index 3999. Yes, fact array size 4001, index 0..4000. So fact[3999] exists. Also invFact[3999] exists? We compute invFact up to 4000. That's fine.
But wait, we compute invFact[maxn] where maxn=4000; then compute downwards. So invFact[3999] = invFact[4000] * 4000 % MOD, etc. So yes.
Thus all good.
Now, confirm that answer for sample 2 we got 4, sample 3 4.
Now, I'm going to think about potential alternative interpretations: Could the stripes be considered as "white stripe" meaning a contiguous block of days with good events, but there might be more than one white stripe? The problem says "first there is a white stripe, then a black one, then a white one again". That's exactly three stripes. So it's fine.
Now, is there any requirement that the events of the same type are indistinguishable? No, they are distinct. So we accounted.
Now, one more nuance: The problem statement: "Even if some events occur on the same day, they go in some order (there are no simultaneous events)." So within a day, the order matters. So our factorial product accounts for that.
Now, final code.
But there is a potential trap: The number of ways to assign good events to white days and order them is w! * C(w-1, d-1). But we must ensure that this counts each assignment to specific white days (i.e., which day is which). Since days are labeled (positions 1..n), and we have a fixed set of white days: they are the days belonging to the white stripes. But note: The white stripes are two separate intervals: first i days, then after black, the last k days. So the white days are not all consecutive; they are in two groups. However, does our method of "cutting a permutation into d segments" correspond to assigning the first segment to day 1, second to day 2, ..., d-th to day d, where day ordering is the natural order of days. But our white days are not day 1..d; they are days 1..i and then days i+j+1..n. So the order of white days is not simply 1..(i+k) if we take all white days in increasing day order: indeed, the white days, when taken in the order of days, are: day1, day2, ..., day i, then day i+j+1, ..., day n. That is exactly a list of d = i+k days in a specific order (by day index). So if we label these white days in that order as positions 1,2,...,d, then the method of cutting a permutation into d segments and assigning segment t to white day t works perfectly. So it's consistent.
Thus the counting for white events using w! * C(w-1, d-1) for d white days is correct, regardless of whether the white days are contiguous or not, as long as they are ordered. The only requirement is that the events assigned to a given day must be contiguous in the overall permutation of good events? Wait, careful: In the overall sequence of all events (good+bad), the good events are not all contiguous because there is a block of bad events in between. However, the ordering of good events themselves, across all white days, is not a contiguous block in the overall sequence; they appear in two separate blocks: all good events from first white stripe appear, then later the good events from second white stripe appear. But if we consider the sequence of good events alone, ignoring bad events, the order of good events is: first all events from first stripe (in day order), then all events from second stripe (in day order). So the overall permutation of good events is the concatenation of the permutations from first stripe days and then second stripe days. That is exactly the same as taking a permutation of all good events (w! possibilities) and cutting it into d segments? No, because the cutting a permutation into d segments yields a sequence where the segments are contiguous in that permutation. But if we take a permutation of all good events and cut it into d segments, and then assign those segments to the d white days in order, we will get a scenario where the good events from the first white stripe (first i days) and the second white stripe (last k days) are concatenated without any interruption; but in the actual timeline, there is a block of bad events between them. However, does that affect the count? The timeline of events is: good events from first stripe (ordered across its days) -> all bad events (ordered across black days) -> good events from second stripe. So the relative order between good events from first and second stripe is fixed: all first-stripe good events occur before any second-stripe good events. That is exactly the same as taking a permutation of all good events and cutting into d segments, but also imposing that the first i segments (first stripe) must come before the last k segments? Actually, when we cut a permutation of all good events into d segments, the order of the segments is the order of the permutation. The permutation itself is a total order of all good events. If we cut it into d segments, the concatenation of those segments yields the original permutation. If we then assign these segments to white days in order, the resulting ordering of good events is exactly that permutation. But does this ordering respect the condition that all events from first stripe (first i days) must come before all events from second stripe (last k days)? In the permutation, the events from the first i segments appear before the events from the last k segments because segments are taken in order. So indeed, the first i segments (which correspond to first i white days) appear before the last k segments (corresponding to last k white days). So the condition is automatically satisfied. Moreover, any assignment of good events to days with ordering that satisfies that all events of first stripe come before all events of second stripe can be represented as a permutation of all good events (by concatenating day orders) and then cutting into d segments where the cut points separate the days. However, is it always possible to represent any such assignment as a permutation cut into d segments? Yes: Given an assignment and ordering, concatenate the orders of white days in day order; that gives a permutation of good events. Then if we cut this permutation at the boundaries between days (i.e., after the events of day1, day2, ..., day i-1, day i, day i+? Actually, after each white day, we have a cut). So it's exactly the cutting. So there's a bijection. So the count w! * C(w-1, d-1) is valid even though the white days are not contiguous in the overall timeline, because we are only considering the good events in isolation; the bad events are inserted later. So independence holds.
Thus product counting is valid.
Now, final step: Implement and test with provided samples.
But note: The modulus is 1e9+9, not 1e9+7. So ensure we use correct constant.
Now, we can produce final solution.
But before finalizing, consider the possibility of integer overflow in multiplication: In C++, we can use long long, but multiplication of two mod numbers (up to 1e9+8) can exceed 2^63? (1e9+9)^2 ≈ 1e18 < 9.22e18, so fits in signed 64-bit. But when we multiply three numbers, e.g., fact[w] up to ~ huge but modded, fact[w] is modulo so < MOD. So product of three numbers < MOD^3 ≈ 1e27, which exceeds 64-bit. So we need to multiply in steps with modulo after each multiplication. We'll do: term = splits % MOD; term = term * nCr(...) % MOD; term = term * nCr(...) % MOD. That's fine. For ans: ans = fact[w]; ans = ans * fact[b] % MOD; ans = ans * sum % MOD. That's fine.
Now, check that nCr returns modulo value.
Now, about precompute: We'll set MAXN = 4000 (maybe 4005). We'll compute up to max(max(w,b), maybe also n? but n not needed). However, we need to access fact[w] and fact[b] which are within max(w,b). So okay.
Potential issue: If w=4000, we need fact[4000]. Our array size must be at least 4000. So we define const int N = 4000; but w can be 4000, so index 4000. So we need size N+1 = 4001. We'll define size as 4005 to be safe.
Now, for nCr, if we pass n=w-1 where w=1? Not possible. So fine.
Now, edge case: n=3, w=4000, b=4000. Then j=1, splits=1, nCr(w-1=3999, n-j-1=1) = 3999, nCr(b-1=3999,0)=1, term=3999. sum=3999. ans = fact[4000] * fact[4000] % MOD * 3999 % MOD. That's a huge number but modulo.
Now, we should ensure that fact[4000] is computed correctly mod 1e9+9. Since 4000! is large but mod prime we can compute.
Now, let's code.
But wait: The problem statement: "It is guaranteed that w + b ≥ n." That ensures total events at least number of days, so there is at least one assignment possible? Not necessarily because white and black events distribution might still be impossible if w < 2? Actually w>=2, but white stripe total days must be at least 2, and we need w >= total white days. Since total white days = n - j, for some j>=1. The condition w+b >= n doesn't guarantee that there exists a j such that both w >= n-j and b >= j. For example, n=5, w=2, b=2, total 4<5? Actually w+b=4<5 violates guarantee. So guarantee ensures total events at least days, but could still have no valid configuration? For n=5, w=2, b=3 => w+b=5≥5. But white days total must be ≤2. For j ranging, we need n-j ≤ w => j ≥ n-w = 3. Also j ≤ b=3. So j=3 possible. So there is configuration. Another example: n=5, w=3, b=2 => j≥2, j≤2 => j=2 works. So maybe always there is at least one configuration under constraints? Let's test borderline: n=3, w=2, b=1 => works. n=3, w=2, b=1 is smallest. Could there be case where w+b ≥ n but no configuration? Suppose n=4, w=2, b=2, works. Suppose n=4, w=3, b=1, works? w+b=4=n, yes works as we computed. Suppose n=4, w=2, b=2, works. Suppose n=4, w=2, b=2 works. Suppose n=4, w=2, b=1? w+b=3<4 not allowed. So with guarantee, there should always be at least one configuration? Let's try n=5, w=2, b=3 => works. n=5, w=1? not allowed because w>=2. So seems always at least one because we can allocate days such that white days <= w and black days <= b? Since w+b >= n, there exists a partition of days into white and black such that white days ≤ w and black days ≤ b? Not necessarily; consider n=5, w=2, b=2 => w+b=4 <5, not allowed. So with w+b >= n, we can always choose j = b (all black days = b) then white days = n - b. Need n - b ≤ w => w ≥ n - b => w + b ≥ n, which is exactly the condition. So setting j = b (if b ≤ n-2? But b may be > n-2? Actually j must be ≤ n-2 because need at least 2 white days. But what if b > n-2? Then we cannot set j = b because j max = n-2. But then we need to check if there exists j between max(1, n-w) and min(b, n-2). Since w+b ≥ n, there should be at least one j satisfying both. Let's verify: We need j such that 1 ≤ j ≤ n-2, j ≤ b, and j ≥ n - w. This interval exists if max(1, n-w) ≤ min(b, n-2). Since w≥2, n-w ≤ n-2 (because w≥2 implies n-w ≤ n-2). Also b ≥ 1. And from w+b ≥ n => b ≥ n-w. Also b could be > n-2? Then min(b, n-2)=n-2. So we need max(1, n-w) ≤ n-2. Since n-w ≤ n-2 holds, we need also max(1, n-w) ≤ n-2, which is true because n-2 ≥ 1? For n=3, n-2=1, so max(1, n-w) could be 1, okay. For n=4, n-2=2, need max(1, n-w) ≤2. If n-w =3? That would require w=1, not allowed. So it's fine. Thus interval non-empty. So answer is always at least one.
Now, think about potential overflow in precomputation: factorial up to 4000 mod prime is fine.
Now, final answer.
But wait: There is also the possibility that the order of events within a day matters, and we have accounted. However, is there any double counting due to the fact that events of different types are distinct, so swapping a good event with a bad event never occurs because they are in different stripes. So fine.
Now, final code.
We'll provide code with comments.
Now, check with provided samples:
Sample 1: Input: 3 2 1 -> Output 2.
Our code: precompute up to max(2,1)=2. fact[2]=2, fact[1]=1. sum loop j=1: splits=1, nCr(1,1)=1, nCr(0,0)=1, sum=1. ans=211=2. Output 2.
Sample 2: Input: 4 2 2 -> max=2. fact[2]=2, fact[2]=2. sum: j=1: splits=2, nCr(1,2)=0; j=2: splits=1, nCr(1,1)=1, nCr(1,1)=1 => sum=1. ans=221=4. Output 4.
Sample 3: Input: 3 2 2 -> max=2. sum: j=1: splits=1, nCr(1,1)=1, nCr(1,0)=1 => sum=1. ans=221=4. Output 4.
Matches.
Now, I'll produce final solution.
Potential improvements: Instead of looping j=1 to n-2, we could loop j = max(1, n-w) to min(b, n-2) to avoid some zero computations, but not necessary.
Now, ensure that we use long long for factorial array.
I will code accordingly.
Now, before final answer, I'll reflect if there is any alternative combinatorial interpretation that might give same formula. Our derivation seems solid.
Now, produce final answer.
But wait: The problem statement: "Polycarpus is sure that his life fits the description: 'first there is a white stripe, then a black one, then a white one again'. So, Polycarpus is sure that this rule is going to fulfill during the next n days. Polycarpus knows that he is in for w good events and b not-so-good events. At least one event is going to take place during each day. As each day is unequivocally characterizes as a part of a white or a black stripe, then each day is going to have events of the same type only (ether good or not-so-good)."
It might be interpreted that the stripes themselves are predetermined lengths? No, they are determined by the days. So we decide which days belong to which stripe, as long as pattern is white, black, white and each stripe length >=1.
Our model matches.
Now, I'll output final solution.
One more check: In our formula, we treat the two white stripes as symmetric? Actually, they are symmetric but we count both ways of splitting white days. That is fine because i and k are distinct positions.
Now, finalize.
But I'm thinking: Could there be an alternative approach using combinatorics: choose positions for black days, etc. That's essentially what we did.
Now, code.
Now, note: The modulus is 1000000009, which is 10^9+9. So define constant.
Now, code with include bits/stdc++.h, using namespace std.
Edge Cases:
- When w=2, b=1, n=3, we are good.
- When w=4000, b=4000, n=4000, then j goes from 1 to 3998, many terms, but nCr(3999, n-j-1) and nCr(3999, j-1) computed quickly. However, we need to ensure we have factorial up to 4000, which we do.
Now, compute factorial up to 4000 using loop.
Potential issue: In precompute we pass maxn = max(w,b). For w=4000,b=4000, maxn=4000, we compute fact[4000] etc. But note that we also need fact[w] = fact[4000]. That's okay.
Now, when we compute nCr(w-1, n-j-1), w-1 = 3999, but our fact array only goes to 4000, so fact[3999] exists. However, we computed invFact up to maxn=4000, which includes invFact[3999] because we compute downwards from 4000. So it's fine.
But what about nCr(b-1, j-1) with b-1=3999, same.
Thus all good.
Now, test with n=4000, w=4000, b=4000. The loop will iterate j=1..3998. For each j, we compute two binomials. The binomial function uses fact[] and invFact[]. That's constant time. So O(4000) operations, fine.
Now, final code.
But one more nuance: In some languages, we might need to precompute factorial up to 4000+something because when we compute nCr with n=3999 and r maybe 1999, we need fact[3999] etc. Already covered.
Now, code.
Now, produce final answer.
But wait: The problem might be from Codeforces Round # ... I recall a problem "White, Black and White Again" maybe. And typical solution uses combinatorics as we did.
Now, I'll produce final solution.
One more check: The modulo is 10^9+9, not 10^9+7. So be careful.
Now, let's code:
#include
#include
#include
using namespace std;
const int MOD = 1000000009;
const int MAX = 4000; // but we will allocate dynamic based on input.
int main() {
int n, w, b;
cin >> n >> w >> b;
int maxN = max(w, b);
vector fact(maxN+1), invFact(maxN+1);
fact[0] = 1;
for (int i = 1; i <= maxN; ++i) fact[i] = fact[i-1] * i % MOD;
// compute inverse factorial using Fermat:
invFact[maxN] = 1;
long long base = fact[maxN];
long long exp = MOD - 2;
long long res = 1;
while (exp) {
if (exp & 1) res = res * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
invFact[maxN] = res;
for (int i = maxN-1; i >= 0; --i) invFact[i] = invFact[i+1] * (i+1) % MOD;
auto nCr = [&](int n, int r) -> long long {
if (r < 0 || r > n) return 0;
return fact[n] * invFact[r] % MOD * invFact[n-r] % MOD;
};
long long sum = 0;
for (int j = 1; j <= n-2; ++j) {
long long splits = n - j - 1;
long long term = splits % MOD;
term = term * nCr(w-1, n - j - 1) % MOD;
term = term * nCr(b-1, j - 1) % MOD;
sum = (sum + term) % MOD;
}
long long ans = fact[w] * fact[b] % MOD * sum % MOD;
cout << ans << endl;
return 0;
}
Now, verify with sample:
Input: 3 2 1 -> maxN=2, compute fact[0..2] = [1,1,2]; invFact[2] = inv of 2 mod MOD, etc. nCr(1,1)=1, nCr(0,0)=1. sum=1, ans=211=2.
Works.
Now, we must ensure that we computed invFact correctly: using modular exponentiation to compute invFact[maxN]. Yes.
Alternatively, we could compute invFact using Fermat for each? But that's fine.
Now, note: If maxN=0? But w>=2, so maxN>=2. So no issue.
Now, check for maxN=4000: computing fact up to 4000 is fine.
Now, one more test: n=3,w=2,b=1 gives ans=2.
Now, finalize.
But there's an alternative simpler expression: sum_{j} (n-j-1) * C(w-1, n-j-1) * C(b-1, j-1). That's our sum.
Now, potential improvement: we could change variable t = n-j-1, then j = n - t - 1, and conditions... but not needed.
Now, final answer.
Now, I'll provide final solution with explanation.
But wait, I realize that the sum may be computed with loops up to n-2, which is at most 3998, but the binomial arguments n-j-1 could be up to n-2, which is up to 3998. If w-1 is, say, 1999, then many nCr(w-1, n-j-1) will be zero when n-j-1 > w-1. But we still compute them. That's fine.
Now, ensure we use long long for all intermediate variables to avoid overflow. Use % MOD.
Now, edge case: n=3, w=2, b=1 works.
Now, final code.
But the problem might require that the answer fits in 64-bit? No, we use mod.
Now, final answer.
I'll produce final answer with explanation. ```cpp
#include
#include
#include
using namespace std;
const int MOD = 1000000009;
int main() {
int n, w, b;
cin >> n >> w >> b;
int maxN = max(w, b);
vector<long long> fact(maxN + 1), invFact(maxN + 1);
// factorials
fact[0] = 1;
for (int i = 1; i <= maxN; ++i)
fact[i] = fact[i - 1] * i % MOD;
// inverse factorial using Fermat
invFact[maxN] = 1;
long long base = fact[maxN];
long long exp = MOD - 2;
while (exp) {
if (exp & 1)
invFact[maxN] = invFact[maxN] * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
for (int i = maxN - 1; i >= 0; --i)
invFact[i] = invFact[i + 1] * (i + 1) % MOD;
auto nCr = [&](int n, int r) -> long long {
if (r < 0 || r > n) return 0;
return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;
};
long long sum = 0;
for (int j = 1; j <= n - 2; ++j) {
long long splits = n - j - 1; // number of ways to split white days
long long term = splits % MOD;
term = term * nCr(w - 1, n - j - 1) % MOD; // good events distribution
term = term * nCr(b - 1, j - 1) % MOD; // bad events distribution
sum = (sum + term) % MOD;
}
long long ans = fact[w] * fact[b] % MOD * sum % MOD;
cout << ans << endl;
return 0;
}