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. [Image] 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:  Somebody adds a new order of some direction with some price. 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: "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.  "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-----

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-----

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


-----Examples-----
Input
6
ADD 1
ACCEPT 1
ADD 2
ACCEPT 2
ADD 3
ACCEPT 3

Output
8

Input
4
ADD 1
ADD 2
ADD 3
ACCEPT 2

Output
2

Input
7
ADD 1
ADD 2
ADD 3
ADD 4
ADD 5
ACCEPT 3
ACCEPT 5

Output
0



-----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.

Difficulty: competition

Starter code:

Solutions:
import heapq

n = int(input())

buy = []  # negative
sell = []
unknown = []

res = 1
for i in range(n):
    cmd, amount = input().strip().split()
    amount = int(amount)
    if cmd == 'ADD':
        if sell and sell[0] < amount:
            heapq.heappush(sell, amount)
        elif buy and -buy[0] > amount:
            heapq.heappush(buy, -amount)
        else:
            unknown.append(amount)
    else:
        if (sell and amount > sell[0]) or (buy and amount < -buy[0]):
            print(0)
            return
        if sell and amount == sell[0]:
            heapq.heappop(sell)
        elif buy and amount == -buy[0]:
            heapq.heappop(buy)
        else:
            res = res * 2 % 1000000007
        for x in unknown:
            if x < amount:
                heapq.heappush(buy, -x)
            elif x > amount:
                heapq.heappush(sell, x)
        unknown = []
res = res * (len(unknown) + 1) % 1000000007
print(res)


from heapq import heapify,heappush,heappop

n=int(input())
low=[]
high=[]
pos=0
mid=set()
for i in range(n):
    #print(high,low,mid)
    s=input().split()
    #print(s)
    x=int(s[1])
    s=s[0]
    #print(s[0],s[0]=='ADD')
    if(s=='ADD'):
        if(len(low) and x<-1*low[0]):
            heappush(low,(-x))
        elif(len(high) and x>high[0]):
            heappush(high,x)
        else:
            mid.add(x)
    else:      
        if(len(low) and x==-low[0]):
            heappop(low)
        elif(len(high) and x==high[0]):
            heappop(high)
        elif(x in mid):        
            pos+=1    
        else:
            print(0)
            return
        for j in mid:
            if(j>x):
                heappush(high,j)
            elif(j<x):
                heappush(low,-j)
        mid=set()
mod=int(1e9+7)
print((pow(2,pos,mod)*(len(mid)+1))%mod)
            
            



from sys import stdin
import heapq
MOD = pow(10, 9) + 7
n=int(stdin.readline())
a=[]
for i in range(n):
    x=stdin.readline().split()
    if x[0]=='ADD':
        a.append((0,int(x[1])))
    else:
        a.append((1,int(x[1])))
next_accept=[-1]*n
accept = -1
for i in range(n-1, -1, -1):
    if a[i][0]== 1:
        accept=i
    next_accept[i] = accept
top = []
bottom = []
buysell_n = 0
last_n=0
invalid = False
for i in range(n):
    if a[i][0] == 0:
        if next_accept[i] != -1:
            if a[i][1] > a[next_accept[i]][1]:
                heapq.heappush(top, a[i][1])
            elif a[i][1] < a[next_accept[i]][1]:
                heapq.heappush(bottom, -a[i][1])
        elif (len(top) == 0 or a[i][1] < top[0]) and (len(bottom) == 0 or a[i][1] > -bottom[0]):
                last_n += 1
    else:
        if len(top) > 0 and a[i][1] == top[0]:
            heapq.heappop(top)
        elif len(bottom) > 0 and a[i][1] == -bottom[0]:
            heapq.heappop(bottom)
        else:
            if len(top) > 0 and a[i][1] > top[0] or len(bottom) > 0 and a[i][1] < -bottom[0]:
                invalid = True
                break
            buysell_n += 1

if invalid:
    ans = 0
else:
    ans = (pow(2, buysell_n, MOD)*(last_n+1))%MOD
print(ans)


import heapq

n = int(input())

ans = 1

mod = 10**9 + 7

buy, undefined, sell = [], [], []

for i in range(n):
    cmd, str_p = input().split()
    p = int(str_p)

    if cmd == 'ADD':
        if buy and p < -buy[0]:
            heapq.heappush(buy, -p)
        elif sell and p > sell[0]:
            heapq.heappush(sell, p)
        else:
            undefined.append(p)
    else:
        if (buy and p < -buy[0]) or (sell and p > sell[0]):
            ans = 0
            break
        elif buy and p == -buy[0]:
            heapq.heappop(buy)
        elif sell and p == sell[0]:
            heapq.heappop(sell)
        else:
            ans = (ans << 1) % mod
        for x in undefined:
            if x < p:
                heapq.heappush(buy, -x)
            elif x > p:
                heapq.heappush(sell, x)
        undefined = []

ans = ans * (len(undefined) + 1) % mod

print(ans)