Find the Number of Possible Ways for an Event - Problem

You are given three integers n, x, and y. An event is being held for n performers.

When a performer arrives, they are assigned to one of the x stages. All performers assigned to the same stage will perform together as a band, though some stages might remain empty.

After all performances are completed, the jury will award each band a score in the range [1, y].

Return the total number of possible ways the event can take place. Since the answer may be very large, return it modulo 10^9 + 7.

Note that two events are considered to have been held differently if either of the following conditions is satisfied:

  • Any performer is assigned a different stage.
  • Any band is awarded a different score.

Input & Output

Example 1 — Basic Case
$ Input: n = 1, x = 2, y = 3
Output: 6
💡 Note: 1 performer can go to stage 1 or 2 (2 ways). Each stage can get score 1, 2, or 3 (3 ways). Total: 2 × 3 = 6 ways.
Example 2 — Multiple Performers
$ Input: n = 2, x = 2, y = 2
Output: 10
💡 Note: Assignments: (1,1), (1,2), (2,1), (2,2). Only 1 stage used: 2 ways × C(2,1) × 2¹ = 8. Both stages used: 2 ways × C(2,2) × 2² = 8. But we subtract overlaps using inclusion-exclusion to get 10.
Example 3 — Single Stage
$ Input: n = 3, x = 1, y = 4
Output: 4
💡 Note: All 3 performers must go to the single stage (1 way). That stage can be scored 1, 2, 3, or 4 (4 ways). Total: 1 × 4 = 4 ways.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ x ≤ 1000
  • 1 ≤ y ≤ 109

Visualization

Tap to expand
Event Organization: 3 Performers → 2 Stages → ScoringP1P2P33 PerformersStage 1Stage 2Score 1-2Score 1-2Total Ways: 2³ assignments × scoring combinations
Understanding the Visualization
1
Input
n=3 performers, x=2 stages, y=2 possible scores
2
Assignment
Each performer chooses one of x stages
3
Scoring
Each active stage (band) gets a score from 1 to y
Key Takeaway
🎯 Key Insight: Multiply assignment possibilities by scoring possibilities, using inclusion-exclusion to count exactly k active stages
Asked in
Google 15 Microsoft 12 Amazon 8
8.2K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen