# for t in range(int(input())):
#     s = input()
#     i, j = 0, 0
#     cnt = 0
#     ans = float('inf')
#     dic = {}
#     while j < len(s):
#         if len(dic) < 3:
#             dic[s[j]] = dic.get(s[j], 0) + 1
#         # print(j)
#         # print(dic)
#         while len(dic) == 3:
#             ans = min(ans, j - i + 1)
#             dic[s[i]] -= 1
#             if dic[s[i]] == 0:
#                 del dic[s[i]]
#             i += 1
#
#         j += 1
#     print((0, ans)[ans < float('inf')])

# for t in range(int(input())):
#     n = int(input())
#     s = list(map(int, input().split()))
#     dp = [1] * n
#     for i in range(n):
#         k = 2
#         while (i + 1) * k <= n:
#             j = (i + 1) * k
#             if s[i] < s[j - 1]:
#                 dp[j - 1] = max(dp[j - 1], dp[i] + 1)
#             k += 1
#     print(max(dp))

# for T in range(int(input())):
#     t = input()
#     z, o = 0, 0
#     for ch in t:
#         z = z + 1 if ch == '0' else z
#         o = o + 1 if ch == '1' else o
#     if z > 0 and o > 0:
#         print('01' * len(t))
#     elif o > 0 and not z:
#         print('1' * len(t))
#     else:
#         print('0' * len(t))

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     a.sort()
#     ans = []
#     while a:
#         ans.append(str(a.pop(len(a) // 2)))
#     print(' '.join(ans))

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     cnt = 0
#     p = set()
#     l, r = 0, sum(a)
#     left, right = {}, {}
#     for i in a:
#         right[i] = right.get(i, 0) + 1
#     for i in range(n - 1):
#         l += a[i]
#         left[a[i]] = left.get(a[i], 0) + 1
#         r -= a[i]
#         right[a[i]] = right.get(a[i], 0) - 1
#         if not right[a[i]]:
#             del right[a[i]]
#         j = n - i - 1
#         if (2 + i) * (i + 1) // 2 == l and (j + 1) * j // 2 == r:
#             if len(left) == i + 1 and len(right) == j:
#                 cnt += 1
#                 p.add((i + 1, n - i - 1))
#     print(cnt)
#     if cnt:
#         for el in p:
#             print(*el)

# for t in range(int(input())):
#     n = int(input())
#     G = []
#     taken = [False] * n
#     girl = -1
#     for i in range(n):
#         g = list(map(int, input().split()))
#         k = g[0]
#         single = True
#         for j in range(1, k + 1):
#             if not taken[g[j] - 1]:
#                 taken[g[j] - 1] = True
#                 single = False
#                 break
#         if single:
#             girl = i
#     if girl == -1:
#         print('OPTIMAL')
#     else:
#         print('IMPROVE')
#         print(girl + 1, taken.index(False) + 1)

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     odd, even = [], []
#     for i in range(2 * n):
#         if a[i] % 2:
#             odd.append(i + 1)
#         else:
#             even.append(i + 1)
#     for i in range(n - 1):
#         if len(odd) >= len(even):
#             print(odd.pop(), odd.pop())
#         else:
#             print(even.pop(), even.pop())

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     a.sort()
#     ans, i, j = 0, 0, 1
#     while j < n:
#         if a[i] < a[j]:
#             ans += 1
#             i += 1
#             j += 1
#         else:
#             while j < n and a[i] == a[j]:
#                 i += 1
#                 j += 1
#     print(ans + 1)

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     got = False
#
#     b = 1
#     while not got and b < 2 * n - 1:
#         if b % 2:
#             i, j = (b - 1) // 2, (b + 1) // 2
#         else:
#             i, j = b // 2 - 1, b // 2 + 1
#         left, right = set(a[:i]), set(a[j:])
#         if left & right:
#             got = True
#         b += 1
#     print('YES' if got else 'NO')

# n, m, k = list(map(int, input().split()))
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# ans = 0
# a, b = [0], [0]
# for el in A:
#     a.append(a[-1] + el)
# for el in B:
#     b.append(b[-1] + el)
# d = [(i, k//i) for i in range(1, int(k**0.5)+1) if k % i == 0]
# d += [(j, i) for i, j in d if i != j]
# for i in range(n):
#     for j in range(m):
#         for q, p in d:
#             if i + q <= n and j + p <= m:
#                 if a[i + q] - a[i] == q and b[j + p] - b[j] == p:
#                     ans += 1
# print(ans)

# for t in range(int(input())):
#     n = int(input())
#     s = input()
#     dic, se = {s: 1}, {s}
#     for k in range(2, n):
#         p = s[k - 1:] + (s[:k - 1], s[:k - 1][::-1])[(n % 2) == (k % 2)]
#         # print(k, p)
#         if p not in dic:
#             # print(dic, p)
#             dic[p] = k
#             se.add(p)
#     if s[::-1] not in dic:
#         dic[s[::-1]] = n
#         se.add(s[::-1])
#     # print(dic)
#     ans = min(se)
#     print(ans)
#     print(dic[ans])

# for t in range(int(input())):
#     a, b, p = list(map(int, input().split()))
#     s = input()
#     road = [a if s[0] == 'A' else b]
#     st = [0]
#     for i in range(1, len(s) - 1):
#         if s[i] != s[i - 1]:
#             road.append(road[-1] + (a, b)[s[i] == 'B'])
#             st.append(i)
#     # print(road)
#     pay = road[-1]
#     j = 0
#     while pay > p and j < len(st):
#         pay = road[-1] - road[j]
#         j += 1
#     # print(j)
#     print(st[j] + 1 if j < len(st) else len(s))

# for t in range(int(input())):
#     n, x, y = list(map(int, input().split()))
#     print(max(1, min(x + y - n + 1, n)), min(n, x + y - 1))

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     print(' '.join(map(str, sorted(a, reverse=True))))

# s = input()
# open, close = [], []
# i = 0
# for i in range(len(s)):
#     if s[i] == '(':
#         open.append(i)
#     else:
#         close.append(i)
# i, j = 0, len(close) - 1
# ans = []
# while i < len(open) and j >= 0 and open[i] < close[j]:
#     ans += [open[i] + 1, close[j] + 1]
#     i += 1
#     j -= 1
# ans.sort()
# print('0' if not ans else '1\n{}\n{}'.format(len(ans), ' '.join([str(idx) for idx in ans])))

import collections
# n, m = list(map(int, input().split()))
# a = list(input() for i in range(n))
# dic = {}
# for w in a:
#     dic[w] = dic.get(w, 0) + 1
# l, r = '', ''
# for i in range(n):
#     for j in range(i + 1, n):
#         # print(i, j, a)
#         if a[i] == a[j][::-1] and dic[a[i]] and dic[a[j]]:
#             l += a[i]
#             r = a[j] + r
#             dic[a[i]] -= 1
#             dic[a[j]] -= 1
# c = ''
# for k, v in dic.items():
#     if v and k == k[::-1]:
#         if c and c[-m] == k or not c:
#             c += k
# print(f'{len(l) + len(c) + len(r)}\n{l + c + r}')

# for t in range(int(input())):
#     n, g, b = list(map(int, input().split()))
#     d = n // 2 + n % 2
#     full, inc = divmod(d, g)
#     ans = (g + b) * (full - 1, full)[inc > 0] + (g, inc)[inc > 0]
#     print(ans if ans >= n else n)

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     a.sort()
#     print(a[n] - a[n - 1])

# for t in range(int(input())):
#     n, x = list(map(int, input().split()))
#     s = input()
#     cntz = s.count('0')
#     total = 2 * cntz - n
#     bal = 0
#     ans = 0
#     for i in range(n):
#         if not total:
#             if bal == x:
#                 ans = -1
#         elif not abs(x - bal) % abs(total):
#             if (x - bal) // total >= 0:
#                 ans += 1
#         bal += 1 if s[i] == '0' else -1
#     print(ans)

# n = int(input())
# ans = 0
# for i in range(1, n + 1):
#     ans += 1 / i
# print(ans)

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     p, s = 0, n - 1
#     for i in range(n):
#         if a[i] < i:
#             break
#         p = i
#     for i in range(n - 1, -1, -1):
#         if a[i] < n - i - 1:
#             break
#         s = i
#     print('Yes' if s <= p else 'No')

# n, k = list(map(int, input().split()))
# a = [input() for i in range(n)]
# c = set(a)
# b = set()
# for i in range(n):
#     for j in range(i + 1, n):
#         third = ''
#         for c1, c2 in zip(a[i], a[j]):
#             if c1 == c2:
#                 third += c1
#             else:
#                 if c1 != 'S' and c2 != 'S':
#                     third += 'S'
#                 elif c1 != 'E' and c2 != 'E':
#                     third += 'E'
#                 else:
#                     third += 'T'
#         if third in c:
#             b.add(frozenset([a[i], a[j], third]))
# print(len(b))

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     total, curr = sum(a), 0
#     ans, i, start = 'YES', 0, 0
#     while ans == 'YES' and i < n:
#         if curr > 0:
#             curr += a[i]
#         else:
#             curr = a[i]
#             start = i
#         # print(curr, i, start, total)
#         if i - start + 1 < n and curr >= total:
#             ans = 'NO'
#         i += 1
#     print(ans)

# for t in range(int(input())):
#     n, p, k = list(map(int, input().split()))
#     a = list(map(int, input().split()))
#     a.sort(reverse=True)
#     odd, even = 0, 0
#     i, j = len(a) - 1, len(a) - 2
#     curr = 0
#     while curr < p and i >= 0:
#         curr += a[i]
#         if curr <= p:
#             odd += 1
#         i -= 2
#     curr = 0
#     while curr < p and j >= 0:
#         curr += a[j]
#         if curr <= p:
#             even += 1
#         j -= 2
#     print(max(odd * 2 - 1, even * 2))

# for t in range(int(input())):
#     s, c = input().split()
#     s = list(ch for ch in s)
#     sor = sorted(s)
#     for i in range(len(s)):
#         if s[i] != sor[i]:
#             j = max(j for j, v in enumerate(s[i:], i) if v == sor[i])
#             s = s[:i] + [s[j]] + s[i + 1:j] + [s[i]] + s[j + 1:]
#             break
#     s = ''.join(s)
#     print(s if s < c else '---')

# for t in range(int(input())):
#     n, s = list(map(int, input().split()))
#     a = list(map(int, input().split()))
#     if sum(a) <= s:
#         print(0)
#     else:
#         curr, i, j = 0, 0, 0
#         for i in range(n):
#             if a[i] > a[j]:
#                 j = i
#             s -= a[i]
#             if s < 0:
#                 break
#         print(j + 1)

# for t in range(int(input())):
#     a, b = list(map(int, input().split()))
#     a, b = (b, a) if b > a else (a, b)
#     if not ((1 + 8 * (a - b))**0.5 - 1) % 2 and ((1 + 8 * (a - b))**0.5 - 1) // 2 >= 0:
#         ans = ((1 + 8 * (a - b))**0.5 - 1) // 2
#         print(int(ans))
#     else:
#         n1 = int(((1 + 8 * (a - b))**0.5 - 1) // 2) + 1
#         while (n1 * (n1 + 1) // 2) % 2 != (a - b) % 2:
#             n1 += 1
#         print(n1)

# for t in range(int(input())):
#     n = int(input())
#     a = list(map(int, input().split()))
#     a.sort()
#     ans = 0
#     l = 0
#     dic = {}
#     for i in range(n - 1, -1, -1):
#         if not a[i] % 2:
#             l, r = 0, 30
#             while l < r:
#                 m = (l + r) // 2
#                 # print(l, r, m, a[i] % 2**m)
#                 if a[i] % 2**m:
#                     r = m
#                 else:
#                     l = m + 1
#             dic[a[i] // 2**(l - 1)] = max(dic.get(a[i] // 2**(l - 1), 0), l - 1)
#     print(sum(list(dic.values())))

# n = int(input())
# s = input()
# b = s.count('B')
# w = n - b
# if b % 2 and w % 2:
#     print(-1)
# elif not b or not w:
#     print(0)
# else:
#     ans = []
#     if not b % 2:
#         for i in range(n - 1):
#             if s[i] != 'W':
#                 ans += [str(i + 1)]
#                 s = s[:i] + 'W' + 'BW'[s[i + 1] == 'B'] + s[i + 2:]
#     elif not w % 2:
#         for i in range(n - 1):
#             if s[i] != 'B':
#                 ans += [str(i + 1)]
#                 s = s[:i] + 'B' + 'WB'[s[i + 1] == 'W'] + s[i + 2:]
#     print(len(ans))
#     print(' '.join(ans))

# n, m = list(map(int, input().split()))
# a = list(map(int, input().split()))
# b = list(map(int, input().split()))
# b.sort()
# ans = float('inf')
# for i in range(n):
#     x = (b[0] - a[i]) % m
#     ax = []
#     for j in range(n):
#         ax.append((a[j] + x) % m)
#     if b == sorted(ax):
#         ans = min(ans, x)
# print(ans)

# for t in range(int(input())):
#     n = int(input())
#     ans = [1] + [0] * (n - 1)
#     p = list(map(int, input().split()))
#     i, j, curr, m = p.index(1), 1, 1, 1
#     l, r = i, i
#     while l >= 0 and r < n:
#         if l and curr + p[l - 1] == (m + 2) * (m + 1) // 2:
#             ans[m] = 1
#             curr += p[l - 1]
#             l -= 1
#
#         elif r + 1 < n and curr + p[r + 1] == (m + 2) * (m + 1) // 2:
#             ans[m] = 1
#             curr += p[r + 1]
#             r += 1
#         else:
#             if l and r + 1 < n:
#                 curr, l, r = ((curr + p[l - 1], l - 1, r),
#                               (curr + p[r + 1], l, r + 1))[p[r + 1] < p[l - 1]]
#             elif not l and r + 1 < n:
#                 curr, l, r = curr + p[r + 1], l, r + 1
#             elif r + 1 == n and l:
#                 curr, l, r = curr + p[l - 1], l - 1, r
#             else:
#                 break
#         m += 1
#     print(''.join([str(i) for i in ans]))

# for t in range(int(input())):
#     n = int(input())
#     p = [input() for i in range(n)]
#     ans = 0
#     for i in range(n):
#         if p[i] in p[i + 1:]:
#             for j in range(10):
#                 code = p[i][:3] + str(j)
#                 if code not in p:
#                     p[i] = code
#                     ans += 1
#                     break
#     print(ans)
#     for code in p:
#         print(code)

# for t in range(int(input())):
#     a, b = list(map(int, input().split()))
#     if (a + b) % 3 == 0 and 2 * min(a, b) >= max(a, b):
#         print('YES')
#     else:
#         print('NO')

# for t in range(int(input())):
#     x, y = list(map(int, input().split()))
#     if (x == 1 and y > 1) or (x == 2 and y > 3) or (x == 3 and y > 3):
#         print('NO')
#     else:
#         print('YES')

# for t in range(int(input())):
#     n, m = list(map(int, input().split()))
#     a = list(map(int, input().split()))
#     if m < n or n == 2:
#         print(-1)
#     elif m == n:
#         print(2 * sum(a))
#         for i in range(n - 1):
#             print(i + 1, i + 2)
#         print(n, 1)
#     else:
#         b = [(a[i], i + 1) for i in range(n)]
#         b.sort()
#         d = m - n
#         ans = sum(a) + d * (b[0][0] + b[1][0])
#         for i in range(d):
#             print(b[0][1], b[1][1])
#         for i in range(n - 1):
#             print(i + 1, i + 2)
#         print(n, 1)

# n = int(input())
# a = list(map(int, input().split()))
# if n % 2:
#     print(-1)
# else:
#     d = 0
#     c = []
#     curr = 0
#     came = set()
#     day = set()
#     inc = False
#     for i in range(n):
#         if a[i] > 0:
#             if a[i] in day:
#                 inc = True
#                 break
#             else:
#                 day.add(a[i])
#                 came.add(a[i])
#         else:
#             if abs(a[i]) not in came:
#                 inc = True
#                 break
#             else:
#                 came.remove(abs(a[i]))
#         if len(came) == 0:
#             d += 1
#             c.append(i + 1)
#             day = set()
#     if len(came) > 0:
#         inc = True
#     if inc:
#         print(-1)
#     else:
#         print(d)
#         print(c[0])
#         for i in range(1, len(c)):
#             print(c[i] - c[i - 1])

# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# x, y = sum(a[:n // 2])**2, sum(a[n // 2:])**2
# print(x + y)

# for t in range(int(input())):
#     n = int(input())
#     r, p, s = list(map(int, input().split()))
#     b = input()
#     S, P, R = b.count('S'), b.count('P'), b.count('R')
#     cnt = 0
#     ans = ''
#     # print(r, 'rock', p, 'paper', s, 'sc')
#     for i in range(n):
#         if b[i] == 'S':
#             if r > 0:
#                 ans, r, cnt = ans + 'R', r - 1, cnt + 1
#             else:
#                 if p > R:
#                     ans, p = ans + 'P', p - 1
#                 if len(ans) < i + 1 and s > P:
#                     ans, s = ans + 'S', s - 1
#             S -= 1
#         elif b[i] == 'P':
#             if s > 0:
#                 ans, s, cnt = ans + 'S', s - 1, cnt + 1
#             else:
#                 if p > R:
#                     ans, p = ans + 'P', p - 1
#                 if len(ans) < i + 1 and r > S:
#                     ans, r = ans + 'R', r - 1
#             P -= 1
#         else:
#             if p > 0:
#                 ans, p, cnt = ans + 'P', p - 1, cnt + 1
#             else:
#                 if s > P:
#                     ans, s = ans + 'S', s - 1
#                 if len(ans) < i + 1 and r > S:
#                     ans, r = ans + 'R', r - 1
#             R -= 1
#     if cnt < (n // 2 + n % 2):
#         print('NO')
#     else:
#         print('YES')
#         # print(r, p, s)
#         print(ans)

# for t in range(int(input())):
#     n = int(input())
#     s = input()
#     f, l = s.find('1'), s.rfind('1')
#     f, l = max(f + 1, n - f) if f != -1 else 0, max(l + 1, n - l) if l != -1 else 0
#     if not f and not l:
#         print(n)
#     else:
#         print(f * 2) if f > l else print(l * 2)

# t = int(input())
# ans = list()
# for _ in [0] * t:
#     n, r = map(int, input().split())
#     x = sorted(set(map(int, input().split())))[::-1]
#     ans.append(sum([x - i * r > 0 for i, x in enumerate(x)]))
# print(' '.join(map(str, ans)))

# n = int(input())
# dots = []
# for i in range(n):
#     dots.append(sum(list(map(int, input().split()))))
# print(max(dots))

# n, m = map(int, input().split())
# print(pow(2**m - 1, n, 10**9 + 7))

# n, k = map(int, input().split())
# s = input()
# if not k:
#     print(s)
# elif n == 1:
#     print('0')
# else:
#     s = [int(i) for i in s]
#     if s[0] > 1:
#         s[0], k = 1, k - 1
#     for i in range(1, n):
#         if not k:
#             break
#         if s[i] > 0:
#             s[i], k = 0, k - 1
#     print(''.join(map(str, s))) if len(s) > 1 else print('0')

# m, n = map(int, input().split())
# r = list(map(int, input().split()))
# c = list(map(int, input().split()))
# grid = [['ok'] * (n + 1) for i in range(m + 1)]
#
# for i in range(m):
#     row = r[i]
#     if row:
#         for j in range(row):
#             grid[i][j] = 1
#         grid[i][row] = 0
#     else:
#         grid[i][row] = 0
#
#
# inv = False
# for j in range(n):
#     col = c[j]
#     if col:
#         for i in range(col):
#             if grid[i][j] == 0:
#                 inv = True
#                 break
#             else:
#                 grid[i][j] = 1
#         if grid[col][j] == 1:
#             inv = True
#             break
#         else:
#             grid[col][j] = 0
#     else:
#         if grid[col][j] == 1:
#             inv = True
#             break
#         else:
#             grid[col][j] = 0
#
# if inv:
#     print(0)
# else:
#     cnt = 0
#     for row in grid[:m]:
#         cnt += row[:n].count('ok')
#     print(pow(2, cnt, 10**9 + 7))

# n = int(input())
# for i in range(n):
#     print('BW' * (n // 2) + 'B' * (n % 2) if i % 2 else 'WB' * (n // 2) + 'W' * (n % 2))

# n = int(input())
# a = list(map(int, input().split()))
# curr, odd, even = 0, 0, 0
# p = 0
# for d in a:
#     odd, even = ((odd, even + 1), (odd + 1, even))[curr % 2]
#     curr += 1 if d < 0 else 0
#     p += odd if curr % 2 else even
# print(n * (n + 1) // 2 - p, p)

# n = int(input())
# a = list(map(int, input().split()))
# p, m, z = 0, [], 0
# for d in a:
#     if d > 0:
#         p += d
#     elif d < 0:
#         m.append(d)
#     else:
#         z += 1
# ans = p - (n - z - len(m))
#
# if len(m) % 2:
#     if z:
#         m.append(-1)
#         ans += 1
#         z -= 1
#     else:
#         m.sort(reverse=True)
#         x = m.pop()
#         ans += 1 - x
#
# mm = len(m)
# ans += abs(sum(m)) - mm
# ans += z
# print(ans)

# n, l, r = map(int, input().split())
# a = [2**i for i in range(r)]
# print(sum(a[:l]) + n - l, sum(a) + (n - r) * a[-1])

# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# print('YES' if not sum(a) % 2 and a[-1] <= sum(a) - a[-1] else 'NO')

# for t in range(int(input())):
#     n, m, k = map(int, input().split())
#     h = list(map(int, input().split()))
#     ans = 'YES'
#     for i in range(n - 1):
#         if abs(h[i] - h[i + 1]) > k:
#             d = h[i] - h[i + 1]
#             if d < 0 and m >= abs(d) - k:
#                 m -= -k - d
#             elif d > 0:
#                 m += min(d + k, h[i])
#             else:
#                 ans = 'NO'
#                 break
#         else:
#             d = h[i] - h[i + 1]
#             if d >= 0:
#                 m += min(d + k, h[i])
#             else:
#                 m += min(k + d, h[i])
#     print(ans)

# h, l = map(int, input().split())
# print((h**2 + l**2) / (2 * h) - h)

# n = int(input())
# a = list(map(int, input().split()))
# a = {i: j for i, j in enumerate(a)}
# m, idx = 0, 0
# ans = 'YES'
# for i in range(n):
#     if a[i] > m:
#         m = a[i]
#         idx = i
# for i in range(1, idx):
#     if a[i] < a[i - 1]:
#         ans = 'NO'
#         break
# for i in range(idx + 1, n):
#     if a[i] > a[i - 1]:
#         ans = 'NO'
#         break
# print(ans)

# n, k = map(int, input().split())
# l, r = 0, n
# while l <= r:
#     m = (l + r) // 2
#     t = n - m
#     if m * (m + 1) // 2 - t == k:
#         ans = t
#         break
#     elif m * (m + 1) // 2 - t < k:
#         l = m + 1
#     else:
#         r = m - 1
# print(ans)

# for t in range(int(input())):
#     n, m = map(int, input().split())
#     grid = [input() for i in range(n)]
#     rows = []
#     cols = [0] * m
#     for row in grid:
#         rows.append(0)
#         for i in range(m):
#             if row[i] == '.':
#                 rows[-1] += 1
#                 cols[i] += 1
#     ans = m + n - 1
#     for i in range(n):
#         for j in range(m):
#             ans = min(ans, rows[i] + cols[j] - (grid[i][j] == '.'))
#     print(ans)

# tiles = input().split()
# unique = {}
# m, p, s = set(), set(), set()
# m_unique = 0
# for t in tiles:
#     unique[t] = unique.get(t, 0) + 1
#     m_unique = max(m_unique, unique[t])
#     if t[1] == 'm':
#         m.add(int(t[0]))
#     elif t[1] == 'p':
#         p.add(int(t[0]))
#     else:
#         s.add(int(t[0]))
# ans = 3 - m_unique
# for t in (m, p, s):
#     if not t:
#         continue
#     else:
#         m_sub = 0
#         l = list(sorted(t))
#         dif = []
#         for i in range(1, len(t)):
#             dif.append(l[i] - l[i - 1])
#             if dif[-1] == 1 or dif[-1] == 2:
#                 m_sub = max(m_sub, 2)
#             if i > 1 and dif[-1] == dif[-2] == 1:
#                 m_sub = 3
#             # print(l, dif, m_sub)
#     ans = min(ans, 3 - m_sub)
# print(ans)

# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# print('NO' if a[-3] + a[-2] <= a[-1] else 'YES')
# if a[-3] + a[-2] > a[-1]:
#     print(' '.join(str(i) for i in a[:-3] + [a[-3]] + [a[-1]] + [a[-2]]))

# n = int(input())
# s = input()
# m = int(input())
# dic = {}
# for i in range(n):
#     dic.setdefault(s[i], []).append(i)
# for t in range(m):
#     name = [ch for ch in input()]
#     c = {}
#     ans = 0
#     # print(t)
#     for i in range(len(name)):
#         idx = c.get(name[i], -1)
#         # print(name[i], dic[name[i]], c)
#         c[name[i]] = idx + 1
#         ans = max(ans, dic[name[i]][idx + 1])
#     print(ans + 1)

# for t in range(int(input())):
#     a = input()
#     b = input()
#     if len(b) < len(a):
#         print('NO')
#     else:
#         ans = 'YES'
#         i, j = 0, 0
#         while i < len(a):
#             cnt1 = 1
#             while i + 1 < len(a) and a[i] == a[i + 1]:
#                 cnt1 += 1
#                 i += 1
#             cnt2 = 0
#             while j < len(b) and b[j] == a[i]:
#                 cnt2 += 1
#                 j += 1
#             if cnt1 > cnt2:
#                 ans = 'NO'
#                 break
#             i += 1
#         print(ans if i == len(a) and j == len(b) else 'NO')

# m, n = map(int, input().split())
# g = [input() for i in range(m)]
# ans = 'YES'
# if m < 3 and n < 3:
#     ans = 'NO'
# if ans == 'YES':
#     c = (-1, -1)
#     for i in range(1, m - 1):
#         for j in range(1, n - 1):
#             if g[i][j] == '*':
#                 if all(g[I][J] == '*' for I, J in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1))):
#                     c = (i, j)
#                     break
#     if c == (-1, -1):
#         ans = 'NO'
#
# if ans == 'YES':
#     plus = {c}
#     I, J = c
#     i, j = I - 1, I + 1
#     while i >= 0 and g[i][J] == '*':
#         plus.add((i, J))
#         i -= 1
#     while j < m and g[j][J] == '*':
#         plus.add((j, J))
#         j += 1
#     i, j = J - 1, J + 1
#     while i >= 0 and g[I][i] == '*':
#         plus.add((I, i))
#         i -= 1
#     while j < n and g[I][j] == '*':
#         plus.add((I, j))
#         j += 1
#
#     for i in range(m):
#         for j in range(n):
#             if g[i][j] == '*' and (i, j) not in plus:
#                 ans = 'NO'
#                 break
#
# print(ans)

# for t in range(int(input())):
#     n = int(input())
#     if not n % 2:
#         print(f'{n // 2} {n // 2}')
#     else:
#         idx = 1
#         for i in range(3, int(n**0.5) + 1):
#             if not n % i:
#                 idx = i
#                 break
#         print(f'{n // idx} {n - n // idx}' if idx > 1 else f'{1} {n - 1}')

# l = int(input())
# n = input()
# i = l // 2
# if not l % 2 and n[i] != '0':
#     print(int(n[:i]) + int(n[i:]))
# else:
#     j = i + 1
#     while n[i] == '0':
#         i -= 1
#     while j < l and n[j] == '0':
#         j += 1
#     if i > 0 and j < l - 1:
#         print(min(int(n[:i]) + int(n[i:]), int(n[:j]) + int(n[j:])))
#     else:
#         if not i:
#             print(int(n[:j]) + int(n[j:]))
#         else:
#             print(int(n[:i]) + int(n[i:]))

# n = int(input())
# a = list(map(int, input().split()))
# b = a.copy()
# for i in range(n):
#     if a[i] >= 0:
#         a[i] = (a[i] + 1) * -1
# if not n % 2:
#     print(' '.join((str(i) for i in a)))
# else:
#     m = (float('inf'), -1)
#     for i in range(n):
#         m = (m, (a[i], i))[m[0] > a[i] and a[i] < -1]
#     if m[1] == -1:
#         b[0] = 0
#         print(' '.join((str(i) for i in b)))
#     else:
#         a[m[1]] = (a[m[1]] + 1) * -1
#         print(' '.join((str(i) for i in a)))

# l = int(input())
# ans = 0
# c = [input() for i in range(l)]
# stack = [1]
# for i in range(l):
#     if c[i] == 'add':
#         ans += stack[-1]
#     elif c[i] == 'end':
#         stack.pop()
#     else:
#         stack.append(min(stack[-1] * int(c[i].split()[1]), 2**32))
#     if ans >= 2**32:
#         ans = 'OVERFLOW!!!'
#         break
# print(ans)

# n = int(input())
# a = list(map(int, input().split()))
# odd = 0
# for d in a:
#     if d % 2:
#         odd += 1
# if 0 < odd < n:
#     print(' '.join((str(i) for i in sorted(a))))
# else:
#     print(' '.join((str(i) for i in a)))

# n = int(input())
# print(n // 2 + 1)
# i, j = 1, 1
# for k in range(n):
#     print(i, j)
#     if i == j:
#         j += 1
#     else:
#         i += 1

# n, m = map(int, input().split())
# a = [list(map(int, input().split())) for i in range(m)]
# x, y = a.pop()
# ans = 'NO'
# for v in (x, y):
#     val = [0] * (n + 1)
#     cnt = 0
#     for v1, v2 in a:
#         if v1 != v and v2 != v:
#             val[v1], val[v2], cnt = val[v1] + 1, val[v2] + 1, cnt + 1
#     if max(val) == cnt:
#         ans = 'YES'
#         break
# print(ans)

# k = int(input())
# idx = 1
# for j in range(5, int(k**0.5) + 1):
#     if not k % j:
#         idx = j
#         break
#
# if idx < 5 or k // idx < 5:
#     print('-1')
# else:
#     pattern = 'aeiou' + 'aeiou'[:max(0, idx - 5)]
#     ans = ''
#     for i in range(k // idx):
#         j = i % len(pattern)
#         ans += pattern[j:] + pattern[:j]
#     print(ans)

# n = int(input())
# u = list(map(int, input().split()))
# g, cnt = [0] * 10, 0
# ans = 0
# for i in range(n):
#     g[u[i] - 1] += 1
#     unique = set(g)
#     unique.discard(0)
#     if len(unique) == 1:
#         k = unique.pop()
#         if k == 1:
#             ans = i
#         elif g.count(k) == 1:
#             ans = i
#     elif len(unique) == 2:
#         if 1 in unique and g.count(1) == 1:
#             ans = i
#         else:
#             a, b = unique
#             if a < b:
#                 b, a = a, b
#             if a - b == 1 and g.count(a) == 1:
#                 ans = i
# print(ans + 1)

# m, n = map(int, input().split())
# a = [list(map(int, input().split())) for i in range(m)]
# b = [list(map(int, input().split())) for j in range(m)]
# ans = 'Possible'
# for i in range(m):
#     j = 0
#     a[i][0], b[i][0] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]]
#     if i and (a[i][j] <= a[i - 1][j] or b[i][j] <= b[i - 1][j]):
#         ans = 'Impossible'
#         break
# if ans == 'Possible':
#     for j in range(n):
#         i = 0
#         a[i][j], b[i][j] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]]
#         if j and (a[i][j] <= a[i][j - 1] or b[i][j] <= b[i][j - 1]):
#             ans = 'Impossible'
#             break
#
# if ans == 'Possible':
#     for i in range(1, m):
#         if ans == 'Possible':
#             for j in range(1, n):
#                 a[i][j], b[i][j] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]]
#                 if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]:
#                     ans = 'Impossible'
#                     break
#                 if b[i][j] <= b[i - 1][j] or b[i][j] <= b[i][j - 1]:
#                     ans = 'Impossible'
#                     break
# print(ans)

# n = int(input())
# a = list(map(int, input().split()))
# ans = float('inf')
# for i in range(n):
#     ans = min(ans, a[i] // max(i, n - i - 1))
# print(ans)

# import string
# alpha = string.ascii_lowercase
# for t in range(int(input())):
#     s = input()
#     a = [ord(ch) - ord('a') for ch in s]
#     a.sort()
#     odd, even = [], []
#     for ch in a:
#         if ch % 2:
#             odd.append(ch)
#         else:
#             even.append(ch)
#     if odd and even:
#         b = odd + even if abs(odd[-1] - even[0]) > 1 else even + odd
#     else:
#         b = (odd, even)[not len(odd)]
#     ans = ''
#     for i in range(1, len(b)):
#         if abs(b[i] - b[i - 1]) == 1:
#             ans = 'No answer'
#             break
#     if ans == '':
#         for i in range(len(b)):
#             ans += alpha[b[i]]
#     print(ans)

# n = int(input())
# s = input()
# eight = s.count('8')
# ans = 'YES'
# if (n - 11) // 2 >= eight or eight == 0:
#     ans = 'NO'
# if ans == 'YES':
#     cnt, e, i = 0, 0, 0
#     while e <= (n - 11) // 2:
#         if s[i] == '8':
#             e += 1
#         else:
#             cnt += 1
#         i += 1
#     if cnt > (n - 11) // 2:
#         ans = 'NO'
# print(ans)

# m, n, h = map(int, input().split())
# front = list(map(int, input().split()))  # n
# left = list(map(int, input().split()))  # m
# up = [list(map(int, input().split())) for i in range(m)]
# for i in range(m):
#     for j in range(n):
#         if up[i][j]:
#             up[i][j] = min(left[i], front[j])
# for row in up:
#     print(' '.join((str(i) for i in row)))

# x = int(input())
# c, ans = 0, []
# for i in range(40):
#     if x == (2**len(format(x, 'b')) - 1):
#         break
#     if i % 2:
#         x += 1
#     else:
#         ans.append(len(format(x, 'b')))
#         x = x ^ (2**ans[-1] - 1)
#     c += 1
# print(c)
# if c:
#     print(' '.join((str(i) for i in ans)))

# m, n = map(int, input().split())
# a = [list(map(int, input().split())) for i in range(m)]
# b = a[0][0]
# ans = [1] * m
# for i in range(1, m):
#     b = b ^ a[i][0]
# for i in range(m):
#     for j in range(n):
#         if b ^ a[i][0] ^ a[i][j] != 0:
#             ans[i] = j + 1
#             print('TAK')
#             exit(print(*ans))
#             break
# print('NIE')

# n = int(input())
# a = [list(ch for ch in input()) for i in range(n)]
# for i in range(n - 2):
#     for j in range(n - 2):
#         if a[i][j] == '#':
#             if all(a[I][J] == '.' for I, J in ((i, j + 1),
#                                                (i + 1, j), (i + 1, j + 1), (i + 1, j + 2),
#                                                (i + 2, j + 1))):
#                 for I, J in ((i, j + 1),
#                              (i + 1, j), (i + 1, j + 1), (i + 1, j + 2),
#                              (i + 2, j + 1)):
#                     a[I][J] = '#'
# for i in range(n):
#     for j in range(n):
#         if a[i][j] == '.':
#             exit(print('NO'))
# print('YES')

# n = input()
# i = len(n) - 1
# k = 1
# for t in n:
#     k = k * int(t)
# ans = k
# while i:
#     if n[i] == '0':
#         n = str(int(n[:i + 1]) - 1) + n[i + 1:]
#     elif n[i] != '9':
#         n = str(int(n[:i + 1]) - int(n[i]) - 1) + n[i + 1:]
#     i -= 1
#     k = 1
#     for t in n:
#         k = k * int(t)
#     ans = max(ans, k)
# print(ans)

for t in range(int(input())):
    n = int(input())
    s = input()
    a = s[0]
    for i in range(1, n):
        if s[i] != a[-1]:
            a += s[i]
    if a[-1] == '>' and len(a) > 1:
        print(1)
    else:
        print(0)


n = int(input())
for i in range(n):
    t = input()
    s = input()
    bef,aft = 0,0
    for j in s:
        if j == '>': break
        bef += 1
    s = s[::-1]
    for j in s:
        if j == '<': break
        aft += 1
    print(min(bef,aft))


for _ in range(int(input())):
    l=int(input())
    s=input()
    c=0
    for i in range(l-1):
        if(s[i]=='<' and s[i+1]=='>'):
            c+=1
    print(c)


for _ in range(int(input())):
    l=int(input())
    s=input()
    c=l-1
    for i in range(l):
        if(s[i]=='>' or s[l-i-1]=='<'):
            c=min(c,i)
    print(c)


rep = int(input())

for i in range(rep):
    a = int(input())
    string = input()

    if(string[0] == "<" and string[-1] == ">"):
        mc = 0
        mf = 0
        for i in range(len(string)):
            if(string[i] == "<"):
                mc += 1
            else:
                break
        for i in range(len(string)-1, -1, -1):
            if(string[i] == ">"):
                mf += 1
            else:
                break
        print(min(mc, mf))

    else:
        print(0)


rep = int(input())

for i in range(rep):
    a = int(input())
    string = input()

    if((string[0] == "<" or string[-1] == ">") and len(string) != 1):
        print(1)
    else:
        print(0)


rep = int(input())

for i in range(rep):
    string = input()

    if(string[0] == "<" or string[-1] == ">"):
        print(1)
    else:
        print(0)


for _ in range(int(input())):
    n = int(input())
    s = input()

    ct = s.count('<')
    if ct == n or ct == 0:
        print(0)
    else:
        ans = 10**9
        ct = 0
        for i in range(n):
            if s[i] != '<':
                break
            ct += 1
        ans = min(ans, ct)
        ct = 0
        for i in range(n):
            if s[-i-1] != '>':
                break
            ct += 1
        ans = min(ans, ct)
        print(ans)


q = int(input())

for _ in range(q):
    n = int(input())
    A = input()
    best = n
    for i in range(n):
        if A[i] == '>':
            best = min([best, i])
    for i in range(n):
        if A[i] == '<':
            best = min([best, n - i - 1])

    print(best)


import sys
import math
from collections import defaultdict,deque

input = sys.stdin.readline
def inar():
    return [int(el) for el in input().split()]
def main():
    t=int(input())
    for _ in range(t):
        n=int(input())
        st=input().strip()
        ans1=10**18
        for i in range(n-1,-1,-1):
            if st[i]=="<":
                if i==n-1:
                    ans1=0
                else:
                    ans1=1
                break
        ans2=10**18
        for i in range(n):
            if st[i]==">":
                if i==0:
                    ans2=0
                else:
                    ans2=1
                break
        print(min(ans1,ans2))

if __name__ == '__main__':
    main()


import sys
import math
from collections import defaultdict,deque

input = sys.stdin.readline
def inar():
    return [int(el) for el in input().split()]
def main():
    t=int(input())
    for _ in range(t):
        n=int(input())
        st=input().strip()
        ans1=10**18
        for i in range(n-1,-1,-1):
            if st[i]=="<":
                ans1=n-i-1
                break
        ans2=10**18
        for i in range(n):
            if st[i]==">":
                ans2=i
                break
        print(min(ans1,ans2))

if __name__ == '__main__':
    main()


# 1140B.py
for _ in range(int(input())):
    n = int(input())
    a = input()
    for i in range(n):
        if a[i]=='>':
            break
    ans = i
    for i in range(n-1,-1,-1):
        if a[i]=='<':
            break
    ans = min(i,n-i)

    print(ans)


# 1140B.py
for _ in range(int(input())):
    n = int(input())
    a = input()
    for i in range(n):
        if a[i]=='>':
            break
    ans = i
    # print(ans)
    for i in range(n-1,-1,-1):
        if a[i]=='<':
            break
    ans = min(ans,n-i)
    # print(i,ans)
    print(ans)


# 1140B.py
for _ in range(int(input())):
    n = int(input())
    a = input()
    for i in range(n):
        if a[i]=='>':
            break
    ans = i
    # print(ans)
    for i in range(n-1,-1,-1):
        if a[i]=='<':
            break
    ans = min(ans,n-i-1)
    # print(i,ans)
    print(ans)


import kotlin.math.min
import kotlin.math.max
import kotlin.math.sqrt

private fun cin_ch() = readLine()!!.split(" ").map{it.toInt()}
private fun cin_array() = readLine()!!.split(" ").map(String::toInt)
private fun cin_string() = readLine()!!.split(" ")

fun main() {
var test = cin_ch()[0];
while (test>0) {
var n = cin_ch()[0];
var s = cin_string()[0];
var ans1 = 0;
var ans2 = 0;
for (i in 0..n-1) {
if (s[i]=='>') {ans1 = i;break;}
}
var point = n - 1;
while (point>=0) {
if (s[point]=='<') break;
point--;
ans2++;
}
println(min(ans1,ans2));
test = test - 1;
}
return;
}


import kotlin.math.min

fun solve(n: Int, s: String): Int {
    var res = n - 1
    for (i in 0..n - 1) {
        if (s[i] == '>' || s[n - i - 1] == '<') {
            res = min(res, i)
        }
    }
    return res
}

fun main(args: Array<String>) {
    val t: Int = readLine()!!.toInt()
    for (i in 1..t) {
        val n: Int = readLine()!!.toInt()
        val s: String = readLine()!!
        println(solve(n, s))
    }
}


import java.util.*

typealias Pii = Pair<Int, Int>
typealias Pll = Pair<Long, Long>

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    var t = nextInt();
    var s = nextLine();
    for (it in 1..t) {
        var t = nextLine();
        var s = nextLine();
        var n = s.length
        var ans = n - 1;
        for (i in 0 until n) {
            if (s[i] == '>') {
                if (i < ans) ans = i;
                break;
            }
        }
        for (i in n - 1 downTo 0) {
            if (s[i] == '<') {
                if (n - 1 - i < ans) ans = n - 1 - i;
                break;
            }
        }
        println("$ans");
    }
}


import java.io.PrintWriter
import java.lang.StringBuilder
import java.util.StringTokenizer
import kotlin.math.*

fun main() {
    output {
        val t = readInt()

        repeat(t) {
            val n = readInt()
            val s = readLn()

            val frt = s.indices.firstOrNull { s[it] == '>' } ?: 0
            val llt = s.indices.lastOrNull { s[it] == '<' } ?: 0

            val ans = minOf(frt, n-1-llt)

            println(ans)
        }
    }
}

/** IO code start */
@JvmField val _reader = System.`in`.bufferedReader()
fun readLine(): String? = _reader.readLine()
fun readLn() = _reader.readLine()!!
@JvmField var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (_tokenizer.hasMoreTokens().not()) _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    return _tokenizer.nextToken()
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }

@JvmField val _writer = PrintWriter(System.out, false)
inline fun output(block: PrintWriter.()->Unit) { _writer.apply(block).flush() }
fun iprintln(o: Any?) { println(o) } // immediate println for interactive, bypasses output{} blocks


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*
import kotlin.math.min

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t = sc.nextInt()
        for (i in (1..t)) {
            val n = sc.nextInt()
            var s = sc.next().trim()

            var start = 0
            var end = 0
            while (s.startsWith("<")) {
                s = s.substring(1)
                start++
            }

            while (s.endsWith(">")) {
                s = s.substring(0, s.length-1)
                end++
            }

            var ans = min(start, end)
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t=sc.nextInt()
        for (i in (1..t)) {
            val n=sc.nextInt()
            var s=sc.next().trim()

            // check if leading < (of any len)
            // check if followed by only > (of any len)
            var ans=0
            if(s.startsWith("<")) {
                while (s.startsWith("<"))
                    s = s.substring(1)
                if(s.startsWith(">"))
                    ans=1
            }
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t=sc.nextInt()
        for (i in (1..t)) {
            val n=sc.nextInt()
            var s=sc.next().trim()

            // check if leading < (of any len)
            // check if followed by only > (of any len)
            var ans=0
            if(s.startsWith("<")) {
                while (s.startsWith("<"))
                    s = s.substring(1)
                if(s.startsWith(">"))
                    ans=1
            }
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*
import kotlin.math.min

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t = sc.nextInt()
        for (i in (1..t)) {
            val n = sc.nextInt()
            var s = sc.next().trim()

            var start = 0
            var end = 0
            while (s.startsWith("<")) {
                s = s.substring(1)
                start++
            }

            while (s.endsWith(">")) {
                s = s.substring(1)
                end++
            }

            var ans = min(start, end)
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t=sc.nextInt()
        for (i in (1..t)) {
            val n=sc.nextInt()
            var s=sc.next().trim()

            // check if leading < (of any len)
            // check if followed by only > (of any len)
            var ans=0
            if(s.startsWith("<")) {
                while (s.startsWith("<"))
                    s = s.substring(1)
                if(s.startsWith(">") && s.indexOf("<")<0)
                    ans=1
            }
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t = sc.nextInt()
        for (i in (1..t)) {
            val n = sc.nextInt()
            var s = sc.next().trim()

            var ans = 0
            if (s.startsWith("<") && s.endsWith(">")) {
                ans = 1
            }
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


//package com.happypeople.codeforces.c1140

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*
import kotlin.math.min

fun main(args: Array<String>) {
    try {
        B1140().run()
    } catch (e: Throwable) {
        println("")
        e.printStackTrace()
    }
}

class B1140 {
    fun run() {
        val sc = Scanner(systemIn())
        val t = sc.nextInt()
        for (i in (1..t)) {
            val n = sc.nextInt()
            var s = sc.next().trim()

            var start = 0
            var end = 0
            while (s.startsWith("<")) {
                s = s.substring(1)
                start++
            }

            while (s.endsWith(">")) {
                s = s.substring(1)
                end++
            }

            var ans =
                    if (start > 0 && end > 0)
                        min(start, end)
                    else
                        0
            println("$ans")
        }
    }

    companion object {
        var inputStr: String? = null

        fun systemIn(): InputStream {
            if (inputStr != null)
                return ByteArrayInputStream(inputStr!!.toByteArray())
            else
                return System.`in`
        }

        var printLog = false
        fun log(str: String) {
            if (printLog)
                println(str)
        }
    }
}


import java.lang.Integer.min

fun main() {
    val tests = readLine()!!.toInt()
    repeat(tests) {
        val length = readLine()!!.toInt()
        val string = readLine()!!
        var prefix = string.indexOf('>')
        if (prefix < 0) prefix = length - 1
        val suffix = length - 1 - string.lastIndexOf('<')
        println(min(prefix, suffix))
    }
}


import java.lang.Integer.min

fun main() {
    val tests = readLine()!!.toInt()
    repeat(tests) {
        val length = readLine()!!.toInt()
        val string = readLine()!!
        var prefix = string.indexOf('>')
        if (prefix < 0) prefix = length - 1
        val suffix = length - string.lastIndexOf('<')
        println(min(prefix, suffix))
    }
}


fun goodString(n:Int, s:String){
    var count = 0
    var i = 0
    var j = s.length - 1

    while(s[i] != '>' && s[j] !='<'){
        count++
        i++
        j--
    }
    println("$count")
}

fun main(args:Array<String>){
    val q = readLine()!!.toInt()

    for(i in 0 until q){
        val n = readLine()!!.toInt()
        val s = readLine()!!
        goodString(n,s)
    }
}


import kotlin.math.min

fun main() {
    val sb = StringBuilder()
    val numCases = readLine()!!.toInt()
    for (i in 1..numCases) {
        readLine()
        val s = readLine()!!
        var leftCounter = -1
        while (++leftCounter < s.length)
            if (s[leftCounter] == '>') break
        var rightCounter = s.length
        while (--rightCounter >= 0)
            if (s[rightCounter] == '<') break
        sb.appendln(min(leftCounter, s.lastIndex - rightCounter))
    }
    print(sb.toString())
}


import kotlin.math.min

fun main() {
    val sb = StringBuilder()
    val numCases = readLine()!!.toInt()
    for (i in 1..numCases) {
        readLine()
        val s = readLine()!!
        var leftCounter = 0
        while (leftCounter++ < s.length)
            if (s[leftCounter] == '>') break
        var rightCounter = s.lastIndex
        while (rightCounter-- >= 0)
            if (s[rightCounter] == '<') break
        sb.appendln(min(leftCounter, s.lastIndex - rightCounter))
    }
    print(sb.toString())
}


import kotlin.math.min

fun main() {
    val sb = StringBuilder()
    val numCases = readLine()!!.toInt()
    for (i in 1..numCases) {
        readLine()
        val s = readLine()!!
        var leftCounter = 0
        while (leftCounter < s.length) {
            if (s[leftCounter] == '>') break
            leftCounter++
        }
        var rightCounter = s.lastIndex
        while (rightCounter >= 0) {
            if (s[rightCounter] == '<') break
            rightCounter--
        }
        rightCounter = s.lastIndex - rightCounter
        sb.appendln(min(leftCounter, rightCounter))
    }
    print(sb.toString())
}


import java.io.*;
import java.util.*;

public class CFB {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    int tc, n;
    String s;
    int solve(String s, int n) {
        int left= 0, right = n;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i) == '>'){
                left = i;
                break;
            }
        }

        for (int i = n-1; i >0 ; i--) {
            if(s.charAt(i) == '<') {
                right = (n-1)-i;
                break;
            }
        }
        return Math.min(left, right);
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() {
        return Integer.parseInt(nextToken());
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }

    public CFB() throws IOException{
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tc = nextInt();
        for (int t = 0; t < tc; t++) {
            int n = nextInt();
            s = nextString();
            out.println(solve(s, n));
        }
        //solve();
        out.close();
    }

    public static void main(String args[]) throws IOException {
        new CFB();
    }
}


import java.io.*;
import java.util.*;

public class CFB {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    int tc, n;
    String s;
    int solve(String s, int n) {
        int left= 0, right = n;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i) == '>'){
                left = i;
                break;
            }
        }

        for (int i = n-1; i >0 ; i--) {
            if(s.charAt(i) == '<') {
                right = (n-1)-i;
            }
        }
        return Math.min(left, right);
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() {
        return Integer.parseInt(nextToken());
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }

    public CFB() throws IOException{
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tc = nextInt();
        for (int t = 0; t < tc; t++) {
            int n = nextInt();
            s = nextString();
            out.println(solve(s, n));
        }
        //solve();
        out.close();
    }

    public static void main(String args[]) throws IOException {
        new CFB();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class probB {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tc = Integer.parseInt(br.readLine());

        for (int i = 0; i < tc; i++) {
            int k = Integer.parseInt(br.readLine());
            String s = br.readLine();
            boolean flag = false;
            if (s.charAt(0) == '<' && s.charAt(k-1) == '>'){
                flag = true;
            }
            System.out.println(flag? '1':'0');
        }

    }
}


//package codeforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Good_String {
    public static void main(String []args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());

        while(t > 0){
            int n = Integer.parseInt(bf.readLine());
            String input = bf.readLine();
            int deletion = getDeletion(input);
            System.out.println(deletion);
            t--;
        }

    }
    public static int getDeletion(String input){
        Stack<Character> stk = new Stack<>();
        /*StringBuilder sb = new StringBuilder();
        for(int i=0; i < input.length(); i++){
            if(i == 0){
                sb.append(input.charAt(i));
            }
            else if( input.charAt(i) != input.charAt(i-1)){
                sb.append(input.charAt(i));
            }
        }
        input = sb.toString();*/
        for(int i=0 ; i < input.length(); i++){
            while(!stk.isEmpty() && input.charAt(i) == '<'){
                stk.pop();
            }
            stk.push(input.charAt(i));
        }
        if(stk.size() == 1) return 0;

        Stack<Character> stk2 = new Stack<>();
        for(int i=input.length()-1 ; i >= 0; i--){
            while(!stk2.isEmpty() && input.charAt(i) == '>'){
                stk2.pop();
            }
            stk2.push(input.charAt(i));
        }
        if(stk2.size() == 1) return 0;
        return Math.min(stk.size()-1, stk2.size()-1);
    }
}


//package codeforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Good_String {
    public static void main(String []args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());

        while(t > 0){
            int n = Integer.parseInt(bf.readLine());
            String input = bf.readLine();
            int deletion = getDeletion(input);
            System.out.println(deletion);
            t--;
        }

    }
    public static int getDeletion(String input){
        Stack<Character> stk = new Stack<>();
        for(int i=0 ; i < input.length(); i++){
            while(!stk.isEmpty() && input.charAt(i) == '<'){
                stk.pop();
            }
            stk.push(input.charAt(i));
        }
        if(stk.size() == 1) return 0;

        Stack<Character> stk2 = new Stack<>();
        for(int i=input.length()-1 ; i >= 0; i--){
            while(!stk2.isEmpty() && input.charAt(i) == '>'){
                stk2.pop();
            }
            stk2.push(input.charAt(i));
        }
        if(stk2.size() == 0) return 0;
        return Math.min(stk.size()/2, stk2.size()/2);
    }
}


//package codeforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Good_String {
    public static void main(String []args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());

        while(t > 0){
            int n = Integer.parseInt(bf.readLine());
            String input = bf.readLine();
            int deletion = getDeletion(input);
            System.out.println(deletion);
            t--;
        }

    }
    public static int getDeletion(String input){
        Stack<Character> stk = new Stack<>();
        StringBuilder sb = new StringBuilder();
        for(int i=0; i < input.length(); i++){
            if(i == 0){
                sb.append(input.charAt(i));
            }
            else if( input.charAt(i) != input.charAt(i-1)){
                sb.append(input.charAt(i));
            }
        }
        input = sb.toString();
        for(int i=0 ; i < input.length(); i++){
            while(!stk.isEmpty() && input.charAt(i) == '<'){
                stk.pop();
            }
            stk.push(input.charAt(i));
        }
        if(stk.size() == 1) return 0;

        Stack<Character> stk2 = new Stack<>();
        for(int i=input.length()-1 ; i >= 0; i--){
            while(!stk2.isEmpty() && input.charAt(i) == '>'){
                stk2.pop();
            }
            stk2.push(input.charAt(i));
        }
        if(stk2.size() == 1) return 0;
        return Math.min(stk.size()/2, stk2.size()/2);
    }
}


//package codeforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Good_String {
    public static void main(String []args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());

        while(t > 0){
            int n = Integer.parseInt(bf.readLine());
            String input = bf.readLine();
            int deletion = getDeletion(input);
            System.out.println(deletion);
            t--;
        }

    }
    public static int getDeletion(String input){
        Stack<Character> stk = new Stack<>();
        StringBuilder sb = new StringBuilder();
        for(int i=0; i < input.length(); i++){
            if(i == 0){
                sb.append(input.charAt(i));
            }
            else if( input.charAt(i) != input.charAt(i-1)){
                sb.append(input.charAt(i));
            }
        }
        input = sb.toString();
        for(int i=0 ; i < input.length(); i++){
            while(!stk.isEmpty() && input.charAt(i) == '<'){
                stk.pop();
            }
            stk.push(input.charAt(i));
        }
        if(stk.size() == 1) return 0;

        Stack<Character> stk2 = new Stack<>();
        for(int i=input.length()-1 ; i >= 0; i--){
            while(!stk2.isEmpty() && input.charAt(i) == '>'){
                stk2.pop();
            }
            stk2.push(input.charAt(i));
        }
        if(stk2.size() == 0) return 0;
        return Math.min(stk.size()/2, stk2.size()/2);
    }
}


package codeforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Good_String {
    public static void main(String []args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());

        while(t > 0){
            int n = Integer.parseInt(bf.readLine());
            String input = bf.readLine();
            int deletion = getDeletion(input);
            System.out.println(deletion);
            t--;
        }

    }
    public static int getDeletion(String input){
        Stack<Character> stk = new Stack<>();
        for(int i=0 ; i < input.length(); i++){
            while(!stk.isEmpty() && input.charAt(i) == '<'){
                stk.pop();
            }
            stk.push(input.charAt(i));
        }
        if(stk.size() == 1) return 0;

        Stack<Character> stk2 = new Stack<>();
        for(int i=input.length()-1 ; i >= 0; i--){
            while(!stk2.isEmpty() && input.charAt(i) == '>'){
                stk2.pop();
            }
            stk2.push(input.charAt(i));
        }
        if(stk2.size() == 0) return 0;
        return Math.min(stk.size()/2, stk2.size()/2);
    }
}


import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {

	public static void main (String[] args) {

	    Scanner sc = new Scanner(Sytsem.in);

	    int t=sc.nextInt();
	    while(t-->0)
	    {

	        	        int n=sc.nextInt();
	        String s=sc.next();

	        int res=n-1;

	        for(int i=0;i<n;i++)
	        {
	            if(s.charAt(i)=='>' || s.charAt(n-i-1)=='<')
	            {
	                res=Math.min(res,i);
	            }

	        }

	        System.out.println(res);

	    }

	}
}


import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {

	public static void main (String[] args) {

	    Scanner sc = new Scanner(System.in);

	    int t=sc.nextInt();
	    while(t-->0)
	    {

	        	        int n=sc.nextInt();
	        String s=sc.next();

	        int res=n-1;

	        for(int i=0;i<n;i++)
	        {
	            if(s.charAt(i)=='>' || s.charAt(n-i-1)=='<')
	            {
	                res=Math.min(res,i);
	            }

	        }

	        System.out.println(res);

	    }

	}
}


import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {

	public static void main (String[] args) {

	    Scanner sc = new Scanner(System.in);

	    int t=sc.nextInt();

	    while(t-->0)
	    {
	        int n=sc.nextInt();
	        String s=sc.next();

	        int count=0;

	        if(n==1)
	        System.out.println(count);

	        else
	        {

	         for(int i=1;i<s.length();i++)
	         {
	             if(s.charAt(i)=='>' && s.charAt(i-1)=='<')
	             {
	                 count++;
	             }

	         }

	         System.out.println(count);
	        }
	    }

	}

}


public class Main
{
	public static void main(String[] args)
	{
		java.util.Scanner scan=new java.util.Scanner(System.in);
		int n=scan.nextInt();
		while(n-->0) {
			int len=scan.nextInt();
			String str=scan.next();
			if(len==1)System.out.println(0);
			else if(str.charAt(0)=='<'&&str.charAt(len-1)=='>')System.out.println(1);
			else System.out.println(0);
		}
	}
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,i,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
/*int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
		//	if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}

		if (!a[k][k])
		{
			fo(j,1,n) if (a[j][k]) break;
			if (j>n) return 0;
			sig=-sig;
			fo(i,1,n) swap(a[k][i],a[j][i]);
		}*/
int det(int K)
{
	int sig=1,ans=1;
	for (int i,j,k=1,Inv,t;k<=K;k++)
	{
		for (i=k;i<=K && !a[i][k];i++);
		//fo(i,1,K) if (a[i][k]) break;
		if (i>K) return 0;
		if (i>k)
		{
			sig=-sig;
			for (j=k;j<=K;j++) swap(a[k][j],a[i][j]);
		}
		Inv=mo-ksm(a[k][k],mo-2);
		for (i=k+1;i<=K;i++)
		{
			t=(ll)Inv*a[i][k]%mo;
			for (j=k;j<=K;j++) a[i][j]=(a[i][j]+(ll)t*a[k][j])%mo;
		}
	}
	for (int i=1;i<=K;i++) ans=(ll)ans*a[i][i]%mo;
	if (sig==-1) ans=-ans;
	if (ans<0) ans+=mo;
	return ans;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	if (d[0]!=e[0])
	{
		printf("-1\n");
		return 0;
	}
	fo(i,1,d[0])
		fo(j,1,d[0])
			a[i][j]=f[d[i]][e[j]];
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
/*int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
		//	if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}*/
void det(int n,int p)
{
    int i,j,k,mark=1;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<=n;k++)
            if(a[k][i])
                break;
        if(k!=i) mark=-mark;
        for(j=1;j<=n;j++)
            swap(a[k][j],a[i][j]);
        for(k=i+1;k<=n;k++)
        {
            long long temp=p-a[k][i]*ksm(a[i][i],p-2)%p;
            for(j=i;j<=n;j++)
                (a[k][j]+=a[i][j]*temp%p)%=p;
        }
    }
    long long ans=1;
    for(i=1;i<=n;i++)
        (ans*=a[i][i])%=p;
    if(mark==-1)
        ans=(p-ans)%p;
    printf("%I64d\n",ans);
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	det(d[0],mo);
	//printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	if (d[0]!=e[0])
	{
		printf("-1\n");
		return 0;
	}
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
			if (!a[i][j]) printf("%d %d\n",i,j);
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
/*int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
		//	if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}*/
int det(int K)
{
	int sig=1,ans=1;
	for (int i,j,k=1,Inv,t;k<=K;k++)
	{
		for (i=k;i<=K && !a[i][k];i++);
		if (i>K) return 0;
		if (i>k)
		{
			sig=-sig;
			for (j=k;j<=K;j++) swap(a[k][j],a[i][j]);
		}
		Inv=mo-ksm(a[k][k],mo-2);
		for (i=k+1;i<=K;i++)
		{
			t=(ll)Inv*a[i][k]%mo;
			for (j=k;j<=K;j++) a[i][j]=(a[i][j]+(ll)t*a[k][j])%mo;
		}
	}
	for (int i=1;i<=K;i++) ans=(ll)ans*a[i][i]%mo;
	if (sig==-1) ans=-ans;
	if (ans<0) ans+=mo;
	return ans;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!--(ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2);
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
			a[i][j]=f[d[i]][e[j]];
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define M 660
using namespace std;
struct abcd{
    int to,next;
}table[100100];
int head[M],tot;
int n,m,k,p;
int degree[M],num[M];
int f[M][M],mo;
bool v[M];
int q[M],g[M];
void Add(int x,int y)
{
    table[++tot].to=y;
    table[tot].next=head[x];
    head[x]=tot;
}
void Topology_Sort()
{
    int i,r=0,h=0;
    for(i=1;i<=n;i++)
        if(!degree[i])
            q[++r]=i;
    while(r!=h)
    {
        int x=q[++h];
        for(i=head[x];i;i=table[i].next)
            if(!--degree[table[i].to])
                q[++r]=table[i].to;
    }
}
void DP(int f[],int x)
{
    int i,j;
    memset(g,0,sizeof g);
    g[x]=1;
    for(j=1;j<=n;j++)
    {
        int x=q[j];
        for(i=head[x];i;i=table[i].next)
            (g[table[i].to]+=g[x])%=p;
    }
    for(i=1;i<=n;i++)
        if(num[i])
            f[num[i]]=g[i];
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!f[i][i])
		{
			fo(j,1,n) if (f[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(f[i][k],f[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*f[j][i]*ksm(f[i][i],mo-2)%mo;
			fo(k,i,n)
				f[j][k]=((f[j][k]-1ll*f[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*f[i][i]%mo;
	return D*xs;
}
int main()
{
    int i,x,y;
    cin>>n>>m>>p;
    mo=p;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        degree[y]++;Add(x,y);
    }
    for(i=1;i<=n;i++)
        if(!degree[i])
            v[i]=true;
    for(i=1;i<=n;i++)
        if(!head[i])
            num[i]=++k;
    Topology_Sort();
    int temp=0;
    for(i=1;i<=n;i++)
        if(v[i])
            DP(f[++temp],i);
    int ans=det(k);
    printf("%d\n",(ans+mo)%mo);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 660
using namespace std;
struct abcd{
    int to,next;
}table[100100];
int head[M],tot;
int n,m,k,p;
int degree[M],num[M];
int f[M][M];
bool v[M];
int q[M],g[M];
void Add(int x,int y)
{
    table[++tot].to=y;
    table[tot].next=head[x];
    head[x]=tot;
}
void Topology_Sort()
{
    int i,r=0,h=0;
    for(i=1;i<=n;i++)
        if(!degree[i])
            q[++r]=i;
    while(r!=h)
    {
        int x=q[++h];
        for(i=head[x];i;i=table[i].next)
            if(!--degree[table[i].to])
                q[++r]=table[i].to;
    }
}
void DP(int f[],int x)
{
    int i,j;
    memset(g,0,sizeof g);
    g[x]=1;
    for(j=1;j<=n;j++)
    {
        int x=q[j];
        for(i=head[x];i;i=table[i].next)
            (g[table[i].to]+=g[x])%=p;
    }
    for(i=1;i<=n;i++)
        if(num[i])
            f[num[i]]=g[i];
}
long long Quick_Power(long long x,int y)
{
    long long re=1;
    while(y)
    {
        if(y&1) (re*=x)%=p;
        (x*=x)%=p; y>>=1;
    }
    return re;
}
void Gauss_Elimination(int n)
{
    int i,j,k,mark=1;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<=n;k++)
            if(f[k][i])
                break;
        if(k!=i) mark=-mark;
        for(j=1;j<=n;j++)
            swap(f[k][j],f[i][j]);
        for(k=i+1;k<=n;k++)
        {
            long long temp=p-f[k][i]*Quick_Power(f[i][i],p-2)%p;
            for(j=i;j<=n;j++)
                (f[k][j]+=f[i][j]*temp%p)%=p;
        }
    }
    long long ans=1;
    for(i=1;i<=n;i++)
        (ans*=f[i][i])%=p;
    if(mark==-1)
        ans=(p-ans)%p;
    cout<<ans<<endl;
}
int main()
{
    //freopen("energy.in","r",stdin);
    //freopen("energy.out","w",stdout);
    int i,x,y;
    cin>>n>>m>>p;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        degree[y]++;Add(x,y);
    }
    for(i=1;i<=n;i++)
        if(!degree[i])
            v[i]=true;
    for(i=1;i<=n;i++)
        if(!head[i])
            num[i]=++k;
    Topology_Sort();
    int temp=0;
    for(i=1;i<=n;i++)
        if(v[i])
            DP(f[++temp],i);
    Gauss_Elimination(k);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
			a[i][j]=f[d[i]][e[j]];
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
		//	if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
/*int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
		//	if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}

		if (!a[k][k])
		{
			fo(j,1,n) if (a[j][k]) break;
			if (j>n) return 0;
			sig=-sig;
			fo(i,1,n) swap(a[k][i],a[j][i]);
		}*/
int det(int K)
{
	int sig=1,ans=1;
	for (int i,j,k=1,Inv,t;k<=K;k++)
	{
		//for (i=k;i<=K && !a[i][k];i++);
		fo(i,1,K) if (a[i][k]) break;
		if (i>K) return 0;
		if (i>k)
		{
			sig=-sig;
			for (j=k;j<=K;j++) swap(a[k][j],a[i][j]);
		}
		Inv=mo-ksm(a[k][k],mo-2);
		for (i=k+1;i<=K;i++)
		{
			t=(ll)Inv*a[i][k]%mo;
			for (j=k;j<=K;j++) a[i][j]=(a[i][j]+(ll)t*a[k][j])%mo;
		}
	}
	for (int i=1;i<=K;i++) ans=(ll)ans*a[i][i]%mo;
	if (sig==-1) ans=-ans;
	if (ans<0) ans+=mo;
	return ans;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!--(ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
int det(int n)
{
	int D=1,xs=1,mult=0;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
			a[i][j]=f[d[i]][e[j]];
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
const int N=600+5,M=1e6+5;
int n,m,mo,x,y,ru[N],chu[N],d[N],e[N],dl[N],q1,q2,a[N][N],D,i,j,f[N][N],p,l,k;
int tt,fst[N],nxt[M],b[M];
void cr(int x,int y)
{
	tt++;
	b[tt]=y;
	nxt[tt]=fst[x];
	fst[x]=tt;
}
int ksm(int x,int y)
{
	int ret=1;
	while (y)
	{
		if (y&1) ret=1ll*ret*x%mo;
		y>>=1;
		x=1ll*x*x%mo;
	}
	return ret;
}
void dp()
{
	while (q1<q2)
		for (p=fst[dl[++q1]];p;p=nxt[p])
			if (!(--ru[b[p]]))
				dl[++q2]=b[p];
	fo(l,1,d[0])
	{
		x=d[l];
		f[x][x]=1;
		fo(i,1,n)
			for(p=fst[(y=dl[i])];p;p=nxt[p])
				(f[x][b[p]]+=f[x][y])%=mo;
	}
}
/*int det(int n)
{
	int D=1,xs=1,mult=0,i,j,k;
	fo(i,1,n)
	{
		if (!a[i][i])
		{
			fo(j,1,n) if (a[j][i]) break;
			if (j>n) return 0;
			xs=-xs;
			fo(k,1,n) swap(a[i][k],a[j][k]);
		}
		fo(j,i+1,n)
		{
			mult=1ll*a[j][i]*ksm(a[i][i],mo-2)%mo;
			fo(k,i,n)
				a[j][k]=((a[j][k]-1ll*a[i][k]*mult)%mo+mo)%mo;
		}
	}
	fo(i,1,n) D=1ll*D*a[i][i]%mo;
	return D*xs;
}*/
int det(int K)
{
	int sig=1,ans=1;
	for (int i,j,k=1,Inv,t;k<=K;k++)
	{
		if (!a[k][k])
		{
			fo(j,1,n) if (a[j][k]) break;
			if (j>n) return 0;
			sig=-sig;
			fo(i,1,n) swap(a[k][i],a[j][i]);
		}
		Inv=mo-ksm(a[k][k],mo-2);
		for (i=k+1;i<=K;i++)
		{
			t=(ll)Inv*a[i][k]%mo;
			for (j=k;j<=K;j++) a[i][j]=(a[i][j]+(ll)t*a[k][j])%mo;
		}
	}
	for (int i=1;i<=K;i++) ans=(ll)ans*a[i][i]%mo;
	if (sig==-1) ans=-ans;
	if (ans<0) ans+=mo;
	return ans;
}
int main()
{
	scanf("%d %d %d",&n,&m,&mo);
	fo(i,1,m)
	{
		scanf("%d %d",&x,&y);
		cr(x,y);
		ru[y]++;
		chu[x]++;
	}
	fo(i,1,n)
	{
		if (!ru[i]) d[++d[0]]=i,dl[++q2]=i;
		if (!chu[i]) e[++e[0]]=i;
	}
	dp();
	fo(i,1,d[0])
		fo(j,1,d[0])
		{
			a[i][j]=f[d[i]][e[j]];
		}
	D=det(d[0]);
	printf("%d",(D+mo)%mo);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define LLD long long
#define N 605
#define M 100005
using namespace std;

int n, m, x, y, mod;
int u[N], p[N];
int l[N], qd[M], qn[M], dou[N], din[N], vis[N], dp[N][N], a[N], b[N];
LLD A[N][N], tmp[N];

void dfs(int x){
	if (vis[x]) return;
	vis[x] = 1;
	if (!dou[x]) dp[x][x] = 1;
	else{
		for (int i=l[x]; i; i=qn[i]){
			dfs(qd[i]);
			for (int j=1; j<=n; j++){
				dp[x][j] += dp[qd[i]][j];
				if (dp[x][j] >= mod) dp[x][j] -= mod;
			}
		}
	}
}

LLD egcd(LLD x, LLD y, LLD &a, LLD &b){
	if (!y){
		a = 1, b = 0;
		return x;
	}
	else{
		LLD t = egcd(y, x%y, b, a);
		b -= x/y * a;
		return t;
	}
}

LLD inv(LLD x, LLD m){
	LLD a, b;
	egcd(x, m, a, b);
	return (a % m + m) % m;
}

int Det(int n, LLD mod){
	int r;
	LLD t, ret;

	FOR(i,0,n){
		r = -1;
		for (int j=0; j<n; j++)
		if (!u[j] && A[j][i]) r = j;
		if (r == -1) return 0;
		u[r] = i + 1;
		p[i] = r;

		t = inv(A[r][i], mod);
		FOR(j,0,n) tmp[j] = (A[r][j] * t) % mod;

		FOR(j,0,n){
			if (j == r || !A[j][i] || u[j]) continue;
			t = inv(A[j][i], mod);
			FOR(k,0,n) A[j][k] -= tmp[k] * t;
		}
	}

	ret = 1;
	FOR(i,0,n) ret = (ret * A[p[i]][i]) % mod;
	FOR(i,0,n)
	FOR(j,0,i)
	if (u[j] > u[i]) ret = mod - ret;
	return ret;
}

int main(){
	scanf("%d%d%d", &n, &m, &mod);
	for (int i=1; i<=m; i++){
		scanf("%d%d", &x, &y);
		qd[i] = y, qn[i] = l[x], l[x] = i;
		din[y]++;
		dou[x]++;
	}

	m = 0;
	for (int i=1; i<=n; i++){
		if (!din[i]) a[m++] = i;
	}
	m = 0;
	for (int i=1; i<=n; i++){
		if (!dou[i]) b[m++] = i;
	}
	sort(a, a + m);
	sort(b, b + m);

	for (int i=1; i<=n; i++) dfs(i);
	FOR(i,0,m)
	FOR(j,0,m) A[i][j] = dp[a[i]][b[j]];

	printf("%d\n", Det(m, mod));
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define LLD long long
#define N 605
#define M 100005
using namespace std;

int n, m, x, y, mod;
int u[N], p[N];
int l[N], qd[M], qn[M], dou[N], din[N], vis[N], dp[N][N], a[N], b[N];
LLD A[N][N], tmp[N];

void dfs(int x){
	if (vis[x]) return;
	vis[x] = 1;
	if (!dou[x]) dp[x][x] = 1;
	else{
		for (int i=l[x]; i; i=qn[i]){
			dfs(qd[i]);
			for (int j=1; j<=n; j++){
				dp[x][j] += dp[qd[i]][j];
				if (dp[x][j] >= mod) dp[x][j] -= mod;
			}
		}
	}
}

LLD egcd(LLD x, LLD y, LLD &a, LLD &b){
	if (!y){
		a = 1, b = 0;
		return x;
	}
	else{
		LLD t = egcd(y, x%y, b, a);
		b -= x/y * a;
		return t;
	}
}

LLD inv(LLD x, LLD m){
	LLD a, b;
	egcd(x, m, a, b);
	return (a % m + m) % m;
}

int Det(int n, LLD mod){
	int r;
	LLD t, ret = 1;

	FOR(i,0,n){
		r = -1;
		for (int j=0; j<n; j++)
		if (!u[j] && A[j][i]) r = j;
		if (r == -1) return 0;
		u[r] = 1;
		p[i] = r;
		ret = (ret * A[r][i]) % mod;

		t = inv(A[r][i], mod);
		FOR(j,0,n) tmp[j] = (A[r][j] * t) % mod;

		FOR(j,0,n){
			if (j < i && p[j] > p[i]) ret = mod - ret;
			if (j == r || !A[j][i] || u[j]) continue;
			t = A[j][i];
			FOR(k,0,n) A[j][k] = (A[j][k] - (tmp[k] * t) % mod + mod) % mod;
		}
	}
	return ret;
}

int main(){
	scanf("%d%d%d", &n, &m, &mod);
	for (int i=1; i<=m; i++){
		scanf("%d%d", &x, &y);
		qd[i] = y, qn[i] = l[x], l[x] = i;
		din[y]++;
		dou[x]++;
	}

	m = 0;
	for (int i=1; i<=n; i++){
		if (!din[i]) a[m++] = i;
	}
	m = 0;
	for (int i=1; i<=n; i++){
		if (!dou[i]) b[m++] = i;
	}
	sort(a, a + m);
	sort(b, b + m);

	for (int i=1; i<=n; i++) dfs(i);
	FOR(i,0,m)
	FOR(j,0,m) A[i][j] = dp[a[i]][b[j]];

	printf("%d\n", Det(m, mod));
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define LLD long long
#define N 605
#define M 100005
using namespace std;

int n, m, x, y, mod;
int u[N], p[N];
int l[N], qd[M], qn[M], dou[N], din[N], vis[N], dp[N][N], a[N], b[N];
LLD A[N][N], tmp[N];

void dfs(int x){
	if (vis[x]) return;
	vis[x] = 1;
	if (!dou[x]) dp[x][x] = 1;
	else{
		for (int i=l[x]; i; i=qn[i]){
			dfs(qd[i]);
			for (int j=1; j<=n; j++){
				dp[x][j] += dp[qd[i]][j];
				if (dp[x][j] >= mod) dp[x][j] -= mod;
			}
		}
	}
}

LLD egcd(LLD x, LLD y, LLD &a, LLD &b){
	if (!y){
		a = 1, b = 0;
		return x;
	}
	else{
		LLD t = egcd(y, x%y, b, a);
		b -= x/y * a;
		return t;
	}
}

LLD inv(LLD x, LLD m){
	LLD a, b;
	egcd(x, m, a, b);
	return (a % m + m) % m;
}

int Det(int n, LLD mod){
	int r;
	LLD t, ret;

	FOR(i,0,n){
		r = -1;
		for (int j=0; j<n; j++)
		if (!u[j] && A[j][i]) r = j;
		if (r == -1) return 0;
		u[r] = i + 1;
		p[i] = r;

		t = inv(A[r][i], mod);
		FOR(j,0,n) tmp[j] = (A[r][j] * t) % mod;

		FOR(j,0,n){
			if (j == r || !A[j][i] || u[j]) continue;
			t = A[j][i];
			FOR(k,0,n) A[j][k] = (A[j][k] - (tmp[k] * t) % mod + mod) % mod;
		}
	}

	ret = 1;
	FOR(i,0,n) ret = (ret * A[p[i]][i]) % mod;
	FOR(i,0,n)
	FOR(j,0,i)
	if (u[j] > u[i]) ret = mod - ret;
	return ret;
}

int main(){
	scanf("%d%d%d", &n, &m, &mod);
	for (int i=1; i<=m; i++){
		scanf("%d%d", &x, &y);
		qd[i] = y, qn[i] = l[x], l[x] = i;
		din[y]++;
		dou[x]++;
	}

	m = 0;
	for (int i=1; i<=n; i++){
		if (!din[i] && dou[i]) a[m++] = i;
	}
	m = 0;
	for (int i=1; i<=n; i++){
		if (!dou[i] && din[i]) b[m++] = i;
	}

	if (m){
		sort(a, a + m);
		sort(b, b + m);

		for (int i=1; i<=n; i++) dfs(i);
		FOR(i,0,m)
		FOR(j,0,m) A[i][j] = dp[a[i]][b[j]];

		printf("%d\n", Det(m, mod));
	}
	else puts("1");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define LLD long long
#define N 605
#define M 100005
using namespace std;

int n, m, x, y, mod;
int u[N], p[N];
int l[N], qd[M], qn[M], dou[N], din[N], vis[N], dp[N][N], a[N], b[N];
LLD A[N][N], tmp[N];

void dfs(int x){
	if (vis[x]) return;
	vis[x] = 1;
	if (!dou[x]) dp[x][x] = 1;
	else{
		for (int i=l[x]; i; i=qn[i]){
			dfs(qd[i]);
			for (int j=1; j<=n; j++){
				dp[x][j] += dp[qd[i]][j];
				if (dp[x][j] >= mod) dp[x][j] -= mod;
			}
		}
	}
}

LLD egcd(LLD x, LLD y, LLD &a, LLD &b){
	if (!y){
		a = 1, b = 0;
		return x;
	}
	else{
		LLD t = egcd(y, x%y, b, a);
		b -= x/y * a;
		return t;
	}
}

LLD inv(LLD x, LLD m){
	LLD a, b;
	egcd(x, m, a, b);
	return (a % m + m) % m;
}

int Det(int n, LLD mod){
	int r;
	LLD t, ret;

	FOR(i,0,n){
		r = -1;
		for (int j=0; j<n; j++)
		if (!u[j] && A[j][i]) r = j;
		if (r == -1) return 0;
		u[r] = i + 1;
		p[i] = r;

		t = inv(A[r][i], mod);
		FOR(j,0,n) tmp[j] = (A[r][j] * t) % mod;

		FOR(j,0,n){
			if (j == r || !A[j][i] || u[j]) continue;
			t = A[j][i];
			FOR(k,0,n) A[j][k] = (A[j][k] - (tmp[k] * t) % mod + mod) % mod;
		}
	}

	ret = 1;
	FOR(i,0,n) ret = (ret * A[p[i]][i]) % mod;
	FOR(i,0,n)
	FOR(j,0,i)
	if (u[j] > u[i]) ret = mod - ret;
	return ret;
}

int main(){
	scanf("%d%d%d", &n, &m, &mod);
	for (int i=1; i<=m; i++){
		scanf("%d%d", &x, &y);
		qd[i] = y, qn[i] = l[x], l[x] = i;
		din[y]++;
		dou[x]++;
	}

	m = 0;
	for (int i=1; i<=n; i++){
		if (!din[i]) a[m++] = i;
	}
	m = 0;
	for (int i=1; i<=n; i++){
		if (!dou[i]) b[m++] = i;
	}
	sort(a, a + m);
	sort(b, b + m);

	for (int i=1; i<=n; i++) dfs(i);
	FOR(i,0,m)
	FOR(j,0,m) A[i][j] = dp[a[i]][b[j]];

	printf("%d\n", Det(m, mod));
	return 0;
}