Read problems statements in Mandarin Chinese  and Russian. 

Our chef Remy ate all the delicious food he made. As a result, he has become really fat :). Chef's assistant Linguini has taken up the 
herculean task of bringing back chef Remy to shape.

Linguini has built a fence which has N planks in a straight line. The planks are numbered 1 to N from left to right. In front of M of these N planks, he has put a bucket of paint. The paints may be of different colors. Each color is represented by any uppercase letter (A-Z). Chef Remy needs to paint the fence. He uses his painting brush to paint the fence. 

Chef Remy starts the task by choosing a plank which has a bucket of paint in front of it and dips his brush in the bucket. He starts walking
randomly along the straight fence passing the planks. He never moves out of the fence. Note that walking randomly along a fence means that Remy keeps walking all the time along his current direction. He can change his direction any time he wishes. Moving along a straight fence has just two directions, forward and backward.

Linguini has devised the following recipe for painting the fence.
Each bucket has an infinite supply of paint. 
The brush can carry paint of a single color at any time. 
When Remy passes a paint bucket, the brush acquires the color in the 
bucket first.  
Every plank that Remy passes in his random walk must be colored with  
the color in the brush. A plank will be colored completely. 
A plank can be colored multiple times. A plank's color is the latest color it 
is painted with. 

A fence coloring is complete when each plank is painted with some color.
Linguini is watching Remy dance around with colors. He wonders how many different complete fence colorings will he be able to see.  Two fence colorings are different if there is atleast one plank numbered x  whose color differs in both the colorings. Find out the number of 
different complete fence colorings modulo 1000000009. 

------ Input ------ 

First line contains T, the number of test cases. T test cases follow. The first line of each case contains N and M
separated by a space. Next M lines follow each of the form x and y separated by a space. Here x is the color in the bucket which is placed at plank 
numbered y.

------  Constraints  ------ 

$ 1 ≤ T ≤ 10 $
$ 1 ≤ N ≤ 100000 $
$ 1 ≤ M ≤ N $
$ x will be an uppercase letter between A to Z $
$ 1 ≤ y ≤ N $

------ Output ------ 

The number of different complete fence colorings modulo 1000000009 for each case on a new line.

----- Sample Input 1 ------ 
3
2 1
R 1
3 1
A 2
6 2
A 2
B 6
----- Sample Output 1 ------ 
1
1
4

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Ad-hoc

Starter code:

Solutions:
m = 10 ** 9 + 9

def solve(arr):
	ans = 1
	for i in range(1, len(arr)):
		if arr[i][1] == arr[i - 1][1]:
			continue
		else:
			ans = ans * (arr[i][0] - arr[i - 1][0]) % m
	return ans
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	arr = []
	for __ in range(b):
		(c, d) = input().split()
		arr.append([int(d), c])
	arr.sort()
	print(solve(arr))


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	s = ['*'] * n
	for i in range(m):
		(c, p) = input().split()
		s[int(p) - 1] = c
	z = ''.join(s).strip('*')
	last = z[0]
	c = 0
	res = 1
	for e in z[1:]:
		if e == '*':
			c += 1
		else:
			if last != e:
				last = e
				res *= c + 1
				res %= 10 ** 9 + 9
			c = 0
	print(res)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	L = [-1] * n
	for i in range(m):
		(a, b) = map(str, input().split())
		L[int(b) - 1] = a
	if m == 1:
		print(1)
		continue
	for i in range(n):
		if L[i] == -1:
			pass
		else:
			f = i
			break
	ans = 1
	initial_co = L[f]
	count = 1
	for i in range(f + 1, n):
		if L[i] == -1:
			count += 1
		elif L[i] == initial_co:
			count = 1
		else:
			ans *= count
			count = 1
			initial_co = L[i]
	print(ans % (10 ** 9 + 9))