Order book
Contest: AIM Tech Round 5 (rated, Div. 1 + Div. 2)
Difficulty rating: 2100
Tags: combinatorics, data structures, greedy

Let's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction (BUY or SELL) and price.

At every moment of time, every SELL offer has higher price than every BUY offer.

In this problem no two ever existed orders will have the same price.

The lowest-price SELL order and the highest-price BUY order are called the best offers, marked with black frames on the picture below.

The presented order book says that someone wants to sell the product at price $$$12$$$ and it's the best SELL offer because the other two have higher prices. The best BUY offer has price $$$10$$$.

There are two possible actions in this orderbook:

1. Somebody adds a new order of some direction with some price.
2. Somebody accepts the best possible SELL or BUY offer (makes a deal). It's impossible to accept not the best SELL or BUY offer (to make a deal at worse price). After someone accepts the offer, it is removed from the orderbook forever.

It is allowed to add new BUY order only with prices less than the best SELL offer (if you want to buy stock for higher price, then instead of adding an order you should accept the best SELL offer). Similarly, one couldn't add a new SELL order with price less or equal to the best BUY offer. For example, you can't add a new offer "SELL $$$20$$$" if there is already an offer "BUY $$$20$$$" or "BUY $$$25$$$" — in this case you just accept the best BUY offer.

You have a damaged order book log (in the beginning the are no orders in book). Every action has one of the two types:

1. "ADD $$$p$$$" denotes adding a new order with price $$$p$$$ and unknown direction. The order must not contradict with orders still not removed from the order book.
2. "ACCEPT $$$p$$$" denotes accepting an existing best offer with price $$$p$$$ and unknown direction.

The directions of all actions are lost. Information from the log isn't always enough to determine these directions. Count the number of ways to correctly restore all ADD action directions so that all the described conditions are satisfied at any moment. Since the answer could be large, output it modulo $$$10^9 + 7$$$. If it is impossible to correctly restore directions, then output $$$0$$$.

Input format:
The first line contains an integer $$$n$$$ ($$$1 \le n \le 363\,304$$$) — the number of actions in the log.

Each of the next $$$n$$$ lines contains a string "ACCEPT" or "ADD" and an integer $$$p$$$ ($$$1 \le p \le 308\,983\,066$$$), describing an action type and price.

All ADD actions have different prices. For ACCEPT action it is guaranteed that the order with the same price has already been added but has not been accepted yet.

Output format:
Output the number of ways to restore directions of ADD actions modulo $$$10^9 + 7$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example each of orders may be BUY or SELL.

In the second example the order with price $$$1$$$ has to be BUY order, the order with the price $$$3$$$ has to be SELL order.

Example input:
6
ADD 1
ACCEPT 1
ADD 2
ACCEPT 2
ADD 3
ACCEPT 3

Example output:
8

Example input:
4
ADD 1
ADD 2
ADD 3
ACCEPT 2

Example output:
2

Editorial:
Keep track of a lower bound for the best sell $$$s_{best}$$$ and an upper bound for the best buy $$$b_{best}$$$ in the order book. If we have ACCEPT $$$p$$$ with $$$p > s_{best}$$$ or $$$p < b_{best}$$$, it's a contradiction and the answer does not exist. If either $$$b_{best} = p$$$ or $$$s_{best} = p$$$, then the answer stay the same, and if $$$b_{best} < p < s_{best}$$$, the answer is multiplied by $$$2$$$, because the removed order could've had any direction. After an ACCEPT $$$p$$$, the price of the order above it is the new $$$s_{best}$$$ and the price of the order below it is the new $$$b_{best}$$$. If in the end there are $$$m$$$ orders with undetermined direction, the answer is multiplied by $$$m+1$$$. To maintain sets of orders with determined direction (i.e. with prices $$$p \le b_{best}$$$ or $$$p \ge s_{best}$$$) one can use std::set or std::priority_queue in C++ or TreeSet or PriorityQueue in Java.