> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/count-all-valid-pickup-and-delivery-options.md).

# Count All Valid Pickup and Delivery Options

Given `n` orders, each order consist in pickup and delivery services.&#x20;

Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).&#x20;

Since the answer may be too large, return it modulo 10^9 + 7.

**Example 1:**

```
Input: n = 1
Output: 1
Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.
```

**Example 2:**

```
Input: n = 2
Output: 6
Explanation: All possible orders: 
(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.
```

**Example 3:**

```
Input: n = 3
Output: 90
```

**Constraints:**

* `1 <= n <= 500`

```java
// Case: n = 1
// Result: 1 (P1 D1)

// Case: n = 2
// Place P2 D2 to 1 result from case n=1: (__ P1 __ D1 __), there are 3 spaces to place to
// The result will be:
// (P2 D2 P1 D1) (P2 P1 D2 D1) (P2 P1 D1 D2)
// (P1 P2 D2 D1) (P1 P2 D1 D2)
// (P1 D1 P2 D2)

// We can come up with the formula

// spaceNum = (n-1)*2 + 1
// result = 1 + 2 + ... + spaceNum = spaceNum * (spaceNum+1) / 2
// Hence,

// spaceNum = (n-1)*2 + 1 = 3
// result = spaceNum * (spaceNum+1) / 2 = 3 * 4 / 2 = 6
// Result: 6

// Case: n = 3
// Place P3 D3 to 6 results from case n=2: (__ P2 __ D2 __ P1 __ D1 __), (__ P2 __ P1 __ D2 __ D1 __),..., (__ P1 __ D1 __ P2 __ D2 __)
// Hence,

// spaceNum = (n-1)*2 + 1 = 5
// result = spaceNum * (spaceNum+1) / 2 = 5 * 6 / 2 = 15
// Apply this P3 D3 place to 6 results from case n = 2
// result = 15 * 6 = 90
class Solution {
    public int countOrders(int n) {
        long MODULO = (long) (1e9 + 7);
        long ans = 1;
        for (int i = 2; i <= n; i++) {
            int spaceNum = (i - 1) * 2 + 1;
            ans *= spaceNum * (spaceNum + 1) / 2;
            ans %= MODULO;
        }
        return (int) ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/count-all-valid-pickup-and-delivery-options.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
