Write Python code for the following task and make the tests pass.

Write a function to pack consecutive duplicates of a given list elements into sublists.

cat > solution.py <<'PY'

from itertools import groupby
def pack_consecutive_duplicates(list1):
    return [list(group) for _, group in groupby(list1)]

PY


Write Python code for the following task and make the tests pass.

Write a function to find the combinations of sums with tuples in the given tuple list. https://www.geeksforgeeks.org/python-combinations-of-sum-with-tuples-in-tuple-list/

git init -q && git config user.email polar@test && git config user.name Polar && git add -A && git commit -qm initial

cat > solution.py <<'PY'

from itertools import combinations
def find_combinations(test_list):
  return [tuple(map(sum, zip(*t))) for t in combinations(test_list, 2)]

PY


Write Python code for the following task and make the tests pass.

Write a python function to check whether the count of divisors is even. https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-24.php

cat > solution.py <<'PY'

import math
def count_divisors(n) :
    cnt = 0
    for i in range(1, (int)(math.sqrt(n)) + 1) :
        if (n % i == 0) :
            if (n / i == i) :
                cnt = cnt + 1
            else :
                cnt = cnt + 2
    return cnt % 2 == 0

PY


Write Python code for the following task and make the tests pass.

Write a python function to find the sum of all odd length subarrays. https://www.geeksforgeeks.org/sum-of-all-odd-length-subarrays/

cat > solution.py <<'PY'

def odd_length_sum(arr):
    sum_ = 0
    n = len(arr)
    for i in range(n):
        # arr[i] occurs (i + 1) * (n - i) times in all subarrays
        times = ((i + 1) * (n - i) + 1) // 2
        sum_ += arr[i] * times
    return sum_

PY


Write Python code for the following task and make the tests pass.

Write a function to find the product of first even and odd number of a given list.

cat > solution.py <<'PY'

def mul_even_odd(list1):
    first_even = next((el for el in list1 if el%2==0),-1)
    first_odd = next((el for el in list1 if el%2!=0),-1)
    return (first_even*first_odd)

PY


Write Python code for the following task and make the tests pass.

Write a function to convert tuple string to integer tuple.

cat > solution.py <<'PY'

def tuple_str_int(test_str):
  return tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', '))

PY


Write Python code for the following task and make the tests pass.

Write a function to locate the right insertion point for a specified value in sorted order.

cat > solution.py <<'PY'

import bisect
def right_insertion(a, x):
    return bisect.bisect_right(a, x)

PY


Write Python code for the following task and make the tests pass.

Write a function that matches a string that has an a followed by three 'b'.

cat > solution.py <<'PY'

import re
def text_match_three(text):
        patterns = 'ab{3}?'
        return re.search(patterns,  text)

PY


Write Python code for the following task and make the tests pass.

Write a function to create a new tuple from the given string and list.

cat > solution.py <<'PY'

def new_tuple(test_list, test_str):
  return tuple(test_list + [test_str])

PY


Write Python code for the following task and make the tests pass.

Write a python function to check whether every even index contains even numbers of a given list.

cat > solution.py <<'PY'

def even_position(nums):
	return all(nums[i]%2==i%2 for i in range(len(nums)))

PY


Write Python code for the following task and make the tests pass.

Write a function to remove tuples from the given tuple.

cat > solution.py <<'PY'

def remove_nested(test_tup):
  return tuple(e for e in test_tup if not isinstance(e, tuple))

PY


Write Python code for the following task and make the tests pass.

Write a python function to count the number of lists in a given number of lists.

cat > solution.py <<'PY'

def count_list(input_list):
    return sum(isinstance(e, list) for e in input_list)

PY


Write Python code for the following task and make the tests pass.

Write a python function to find the last position of an element in a sorted array.

cat > solution.py <<'PY'

def last(arr,x):
    return len(arr)-arr[::-1].index(x) - 1

PY


Write Python code for the following task and make the tests pass.

Write a function that matches a string that has an 'a' followed by anything, ending in 'b'.

cat > solution.py <<'PY'

import re
def text_starta_endb(text):
    patterns = 'a.*?b$'
    return re.search(patterns,  text)

PY


Write Python code for the following task and make the tests pass.

Write function to find the sum of all items in the given dictionary.

cat > solution.py <<'PY'

def return_sum(d):
  return sum(d.values())

PY


Write Python code for the following task and make the tests pass.

Write a python function to find the sum of all odd natural numbers within the range l and r.

cat > solution.py <<'PY'

def sum_odd(n):
    terms = (n + 1) // 2
    sum1 = terms * terms
    return sum1
def sum_in_range(l,r):
    return sum_odd(r) - sum_odd(l - 1)

PY


Write Python code for the following task and make the tests pass.

Write a python function to find the sum of an array.

cat > solution.py <<'PY'

def _sum(arr):
    return sum(arr)

PY


Write Python code for the following task and make the tests pass.

Write a function to that rotate left bits by d bits a given number. We assume that the number is 32 bit.

cat > solution.py <<'PY'

def left_rotate(n,d):
    INT_BITS = 32
    return (n << d)|(n >> (INT_BITS - d))

PY


Write Python code for the following task and make the tests pass.

Write a function to remove all whitespaces from a string.

cat > solution.py <<'PY'

def remove_all_spaces(text):
 return text.replace(' ', '')

PY


Write Python code for the following task and make the tests pass.

Write a python function to count the number of equal numbers from three given integers.

cat > solution.py <<'PY'

def test_three_equal(x,y,z):
  result = set([x,y,z])
  if len(result) == 3:
    return 0
  elif len(result) == 2:
    return 2
  else:
    return 3

PY


Write Python code for the following task and make the tests pass.

Write a function to check whether the given number is a perfect square or not. https://www.geeksforgeeks.org/check-if-given-number-is-perfect-square-in-cpp/

cat > solution.py <<'PY'

def is_perfect_square(n) :
    if n < 0:
        return False
    return n**(1/2) == int(n**(1/2))

PY


Write Python code for the following task and make the tests pass.

Write a function to check whether the product of numbers in a list is even or not.

cat > solution.py <<'PY'

def is_product_even(arr):
    return any(x % 2 == 0 for x in arr)

PY


Write Python code for the following task and make the tests pass.

Write a function that returns the list in a list of lists whose sum of elements is the highest.

cat > solution.py <<'PY'

def max_sum_list(lists):
 return max(lists, key=sum)

PY


Write Python code for the following task and make the tests pass.

Write a function to find maximum run of uppercase characters in the given string.

cat > solution.py <<'PY'

def max_run_uppercase(test_str):
  cnt = 0
  res = 0
  for idx in range(0, len(test_str)):
    if test_str[idx].isupper():
      cnt += 1
    else:
      res = cnt
      cnt = 0
  if test_str[len(test_str) - 1].isupper():
    res = cnt
  return res

PY


Write Python code for the following task and make the tests pass.

Write a python function to find the first odd number in a given list of numbers.

cat > solution.py <<'PY'

def first_odd(nums):
  first_odd = next((el for el in nums if el%2!=0), None)
  return first_odd

PY


Write Python code for the following task and make the tests pass.

Write a function to check if the given tuples contain the k or not.

cat > solution.py <<'PY'

def check_K(test_tup, K):
  return K in test_tup

PY


Write Python code for the following task and make the tests pass.

Write a function to check if each element of second tuple is smaller than its corresponding element in the first tuple.

cat > solution.py <<'PY'

def check_smaller(test_tup1, test_tup2):
  return all(x > y for x, y in zip(test_tup1, test_tup2))

PY


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
The annual "Rock Jumping" competition is about to begin!

This competition takes place in a straight river channel, where some giant rocks are distributed. The organizers have chosen two rocks as the starting point and the endpoint of the competition. Between the start and endpoint, there are $N$ rocks (excluding the starting and endpoint rocks). During the competition, participants will start at the starting point and jump to adjacent rocks step by step until they reach the endpoint.

To make the competition more challenging, the organizers plan to remove some of the rocks, trying to maximize the shortest jump distance during the competition. Due to budget constraints, the organizers can remove at most $M$ rocks lying between the starting and endpoint (the start and endpoint rocks cannot be removed).

### Input Format
The first line of the input file contains three integers $L$, $N$, and $M$, representing the distance between the starting point and the endpoint, the number of rocks between the start and endpoint, and the maximum number of rocks that can be removed, respectively.

The next $N$ lines each contain an integer $D_i$ ($0 < D_i < L$), where the $i^{th}$ line indicates the distance of the $i^{th}$ rock from the starting point. These rocks are given in ascending order of distance from the starting point, and no two rocks share the same position.

### Output Format
The output file contains a single integer, which is the maximum possible value of the shortest jump distance.

### Example

**Data Range and Hints**
For $20\%$ of the data, $0 \leq M \leq N \leq 10$.
For $50\%$ of the data, $0 \leq M \leq N \leq 100$.
For $100\%$ of the data, $0 \leq M \leq N \leq 50,000$, $1 \leq L \leq 1,000,000,000$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

To celebrate the successful opening of NOI, the organizers have prepared a sushi banquet. Xiao G and Xiao W, as contestants of NOI, are also invited to the sushi banquet.

At the banquet, the organizers provide $n-1$ different types of sushi, numbered $1, 2, 3, \ldots, n-1$. The deliciousness of the $i$-th type of sushi is $i+1$ (hence the deliciousness ranges from $2$ to $n$).

Now, Xiao G and Xiao W would like to each choose some types of sushi to taste. They define a tasting plan as **disharmonious** if and only if: there exists a sushi with a deliciousness value $x$ among the types of sushi Xiao G tastes, and there exists a sushi with a deliciousness value $y$ among the types of sushi Xiao W tastes, such that $x$ and $y$ are not coprime.

Xiao G and Xiao W now want to count the total number of **harmonious** sushi tasting plans (modulo a given positive integer $p$). Note that it is possible for either person to not taste any sushi.

---

### Input Format

The first line of the input contains two positive integers $n$ and $p$, separated by a single space, indicating that there are $n$ types of sushi in total, and the total number of harmonious plans should be computed modulo $p$.

---

### Output Format

Output a single line containing one integer, representing the result of the harmonious plans modulo $p$.

---

### Sample

---

### Constraints and Hints

$2 \leq n \leq 500, \ 0 < p \leq 1000000000$

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
In a directed graph $ G $, where the length of each edge is $ 1 $, you are given a starting point and an ending point. Your task is to find a path in the graph from the starting point to the ending point that satisfies the following conditions:

1. Every vertex in the path must have outgoing edges pointing to vertices that are directly or indirectly connected to the ending point.
2. Among paths that satisfy condition 1, the path must be the shortest.

Note: The graph $ G $ may contain duplicate edges and self-loops. It is guaranteed that the ending point has no outgoing edges.

Output the length of the path that meets the above conditions.

---

### Input Format
The first line contains two integers $ n $ and $ m $, separated by a space, representing that the graph has $ n $ vertices and $ m $ edges.

The next $ m $ lines each contain two integers $ x $ and $ y $, separated by a space, indicating an edge going from vertex $ x $ to vertex $ y $.

The last line contains two integers $ s $ and $ t $, separated by a space, where $ s $ is the starting point and $ t $ is the ending point.

---

### Output Format
Output a single integer representing the length of the shortest path that satisfies the conditions described in the problem. If no such path exists, output $ -1 $.

---

### Example 1

---

### Example 2

---

### Constraints and Hints
- For $ 30\% $ of the cases: $ 0 < n \le 10 $, $ 0 < m \le 20 $.
- For $ 60\% $ of the cases: $ 0 < n \le 100 $, $ 0 < m \le 2000 $.
- For $ 100\% $ of the cases: $ 0 < n \le 10000 $, $ 0 < m \le 200000 $, $ 0 < x, y, s, t \le n $, $ x,s \ne t $.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

The Chun Chun Kindergarten is hosting its annual "Building Block Competition." This year's challenge is to construct a building with a width of $n$. The building can be considered as consisting of $n$ blocks, each with a width of $1$. The final height of the $i$-th block should be $h_i$.

At the beginning of the construction process, no blocks are present (it can be regarded as $n$ blocks with a height of $0$). During each operation, the children can select a continuous interval $[L, R]$, and increase the height of all blocks from the $L$-th block to the $R$-th block (inclusive) by $1$.

Little M is a clever child, and she quickly devised the optimal strategy to minimize the number of operations needed to construct the building. However, she is not a fan of executing her plans, so she asked you to implement the strategy and calculate the minimum number of operations required.

### Input Format

The input contains two lines:

1. The first line contains an integer $n$, representing the width of the building.
2. The second line contains $n$ integers, where the $i$-th integer is $h_i$.

### Output Format

Output only one line, the minimum number of operations required to construct the building.

### Example

One feasible optimal strategy is to successively choose $[1, 5], [1, 3], [2, 3], [3, 3], [5, 5]$.

### Constraints and Tips

- For 30% of the data, $1 \leq n \leq 10$.
- For 60% of the data, $1 \leq n \leq 1\,000$.
- For 100% of the data, $1 \leq n \leq 100\,000$, $0 \leq h_i \leq 10\,000$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
Given a polynomial equation:

$$a_0 + a_1x + a_2x^2 + \cdots + a_nx^n = 0$$

Find all integer solutions within the range $[1, m]$ where $n$ and $m$ are positive integers.

### Input Format
The input consists of $n + 2$ lines.
- The first line contains two integers $n$ and $m$, separated by a space.
- The next $n + 1$ lines each contain one integer, representing the coefficients $a_0, a_1, a_2, \ldots, a_n$ in order.

### Output Format
- On the first line, output the number of integer solutions to the equation within the interval $[1, m]$.
- Then, each subsequent line should output one integer solution within $[1, m]$, listed in increasing order.

### Example 1

#### Example 2

#### Example 3

### Constraints and Hints
- For $30\%$ of the test cases: $0 < n \leq 2$, $|a_i| \leq 100$, $a_n \neq 0$, $m < 100$.
- For $50\%$ of the test cases: $0 < n \leq 100$, $|a_i| \leq 10^{100}$, $a_n \neq 0$, $m < 100$.
- For $70\%$ of the test cases: $0 < n \leq 100$, $|a_i| \leq 10^{10000}$, $a_n \neq 0$, $m < 10^4$.
- For $100\%$ of the test cases: $0 < n \leq 100$, $|a_i| \leq 10^{10000}$, $a_n \neq 0$, $m < 10^6$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
An undirected connected graph $G$ consists of $n$ nodes and $n - 1$ edges. The nodes are numbered from $1$ to $n$, and the weight of the node numbered $i$ is $W_i$. The length of each edge is $1$. The **distance** between two nodes $(u, v)$ on the graph is defined as the shortest path distance from node $u$ to node $v$. For a pair of nodes $(u, v)$ on graph $G$, if the distance between them is $2$, then they will generate a **joint weight** equal to $W_v \times W_u$.

What is the **maximum joint weight** among all possible ordered pairs in graph $G$? Additionally, what is the **sum of all joint weights**?

### Input Format
The first line contains a single integer $n$.

The next $n - 1$ lines each contain $2$ positive integers $u, v$, separated by a space, indicating that there is an edge between node $u$ and node $v$.

The last line contains $n$ positive integers separated by a space, where the $i^{th}$ integer represents the weight $W_i$ of the node numbered $i$ in graph $G$.

### Output Format
Output a single line containing 2 integers separated by a space: the maximum joint weight in graph $G$ and the sum of all joint weights.
**Since the sum of all joint weights might be very large, the result should be taken modulo $10007$.**

### Example

**Input:**
```
4
1 2
2 3
1 4
2 4 3 1
```

**Output:**
```
8 17
```

### Constraints and Hints
- For $30\%$ of the data, $1 < n \leq 100$.
- For $60\%$ of the data, $1 < n \leq 2000$.
- For $100\%$ of the data, $1 < n \leq 200000$, $0 < W_i \leq 10000$.

It is guaranteed that at least one valid ordered pair exists in the graph that can generate a joint weight.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

We know that a rooted tree can be traversed using Depth First Search (DFS) and Breadth First Search (BFS), yielding its DFS sequence and BFS sequence respectively. The DFS sequence of two different trees can be identical, and their BFS sequence can also be identical. For instance, the DFS sequence of the two trees below is `1 2 4 5 3` and their BFS sequence is `1 2 3 4 5`.

![](https://i.loli.net/2018/06/10/5b1ce8c6521c4.png)

Given a DFS sequence and a BFS sequence, we want to know the average height of all possible rooted trees that satisfy these sequences. That is, if there are $K$ different rooted trees with the given DFS and BFS sequences, and their heights are $h_1, h_2, \ldots, h_K$, please output:

$$
\frac{h_1 + h_2 + \ldots + h_K}{K}
$$

---

### Input Format

- The first line contains a positive integer $n$, representing the number of nodes in the tree.
- The second line contains $n$ positive integers, which form a permutation of $1 \ldots n$, representing the DFS sequence of the tree.
- The third line contains $n$ positive integers, which form a permutation of $1 \ldots n$, representing the BFS sequence of the tree.

It is guaranteed that there is at least one tree that satisfies the given DFS and BFS sequences.

---

### Output Format

Output a single real number, rounded to exactly three decimal places, representing the average height of the trees.

---

### Examples

#### Example Input 1
```plain
5
1 2 4 5 3
1 2 3 4 5
```

#### Example Output 1
```plain
3.500
```

#### Example Input and Output 2
Refer to the files `count.in` and `count.ans` in the additional attachment.

---

### Constraints and Notes

- If the absolute difference between the output and the correct answer does not exceed $0.001$, the test point will be considered passed; otherwise, it will not be scored.
- For $20\%$ of the test cases, $n \leq 10$.
- For $40\%$ of the test cases, $n \leq 100$.
- For $85\%$ of the test cases, $n \leq 2000$.
- For $100\%$ of the test cases, $2 \leq n \leq 200,000$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
There are two strings $ A $ and $ B $ that only consist of lowercase English letters. The task is to extract $ k $ non-overlapping, non-empty substrings from string $ A $ and concatenate them in the order in which they appear in string $ A $ to form a new string. Determine how many ways it is possible for this new string to be equal to string $ B $.

**Note**: Different positions of substring extraction are considered different solutions.

### Input Format
The first line contains three positive integers $ n, m, k $, which represent the length of string $ A $, the length of string $ B $, and the value of $ k $ as mentioned in the problem description. Each pair of integers is separated by a single space.
The second line contains a string of length $ n $, representing string $ A $.
The third line contains a string of length $ m $, representing string $ B $.

### Output Format
Output a single integer in one line, representing the number of solutions. Since the result could be very large, output the result modulo $ 1,000,000,007 $.

### Sample Input and Output

#### Sample 1:

#### Sample 2:

#### Sample 3:

---

### Constraints and Hints
For the $ 1^{st} $ dataset: $ 1 \leq n \leq 500, 1 \leq m \leq 50, k = 1 $;
For the $ 2^{nd} $ to $ 3^{rd} $ datasets: $ 1 \leq n \leq 500, 1 \leq m \leq 50, k = 2 $;
For the $ 4^{th} $ to $ 5^{th} $ datasets: $ 1 \leq n \leq 500, 1 \leq m \leq 50, k = m $;
For the $ 1^{st} $ to $ 7^{th} $ datasets: $ 1 \leq n \leq 500, 1 \leq m \leq 50, 1 \leq k \leq m $;
For the $ 1^{st} $ to $ 9^{th} $ datasets: $ 1 \leq n \leq 1000, 1 \leq m \leq 100, 1 \leq k \leq m $;
For all $ 10 $ datasets: $ 1 \leq n \leq 1000, 1 \leq m \leq 200, 1 \leq k \leq m $.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Statement

Xiaoyuan and Xiaoxuan are good friends and classmates who always have endless topics to discuss. During one team-building activity, their classmates arranged themselves in a matrix of $m$ rows and $n$ columns. Xiaoyuan and Xiaoxuan were seated at the two ends of the diagonal of the matrix, making it impossible for them to converse directly. Fortunately, they could communicate by passing a note. The note must be passed through several classmates to reach the other person. Xiaoyuan is seated at the top-left corner of the matrix at coordinate $(1,1)$, while Xiaoxuan is seated at the bottom-right corner at coordinate $(m,n)$. The note passed from Xiaoyuan to Xiaoxuan can only be moved down or to the right, and the note passed from Xiaoxuan to Xiaoyuan can only be moved up or to the left.

During the activity, Xiaoyuan wants to send a note to Xiaoxuan, and at the same time, Xiaoxuan wants to reply with a note to Xiaoyuan. Every classmate can help pass the notes, but they will only help once. In other words, if a classmate helps pass the note from Xiaoyuan to Xiaoxuan, they will not help pass the note back from Xiaoxuan to Xiaoyuan, and vice versa.

Another important detail to note is that every classmate has varying levels of willingness to help, expressed as their kindness level. The kindness levels (note: those of Xiaoyuan and Xiaoxuan are undefined and represented by $0$ in the input) are represented by natural numbers in the range $[0, 100]$, where larger numbers indicate higher kindness levels. Xiaoyuan and Xiaoxuan hope to find the classmates with the highest kindness levels to help pass the notes. Your task is to identify two paths (one for each note) such that the sum of kindness levels of the classmates involved in both paths is maximized.

### Input Format

The first line contains two integers $m$ and $n$, separated by a space, indicating that the class has a matrix of $m$ rows and $n$ columns.

The next $m$ lines describe an $m \times n$ matrix, where the integer in the $i$th row and $j$th column represents the kindness level of the student seated at $(i,j)$. The $n$ integers in each row are separated by a space.

### Output Format

Output a single integer in one line, representing the maximum possible sum of kindness levels of students involved in the two paths for passing the notes back and forth.

### Example

#### Input:
```
4 4
5 1 2 4
8 3 6 7
2 4 7 9
6 7 4 9
```

#### Output:
```
67
```

### Constraints and Hints

- For $30\%$ of the data, $1 \leq m,n \leq 10$.
- For $100\%$ of the data, $1 \leq m,n \leq 50$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

A magic square is a fascinating $N \times N$ matrix: it consists of the numbers $1, 2, 3, \ldots, N \times N$ and satisfies the property that the sum of the numbers in each row, each column, and both diagonals is the same.

When $N$ is an odd number, a magic square can be constructed using the following method:
First, write the number $1$ in the middle of the first row.
Then, fill each successive number $K$ ($K = 2, 3, \ldots, N \times N$) as follows:

1. If $(K - 1)$ is in the first row but not in the last column, place $K$ in the last row and in the column immediately to the right of the column containing $(K - 1)$;
2. If $(K - 1)$ is in the last column but not in the first row, place $K$ in the first column and in the row immediately above the row containing $(K - 1)$;
3. If $(K - 1)$ is in the last column of the first row, place $K$ directly below $(K - 1)$;
4. If $(K - 1)$ is neither in the first row nor in the last column, and the position diagonally up and to the right of $(K - 1)$ has not been filled, then place $K$ in that position; otherwise, place $K$ directly below $(K - 1)$.

Given $N$, construct an $N \times N$ magic square according to the method described above.

---

### Input Format

The input consists of a single line containing an integer, which is the size of the magic square.

---

### Output Format

Output $N$ lines, each containing $N$ integers, representing the magic square constructed using the method described above. Adjacent integers in the same row should be separated by a single space.

---

### Example

```
Input:
3

Output:
8 1 6
3 5 7
4 9 2
```

---

### Constraints and Notes

For $100\%$ of the test cases, $1 \leq N \leq 39$, and $N$ is an odd number.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

Given a polynomial $(ax+by)^k$, find the coefficient of the term $x^ny^m$ after expanding the polynomial.

### Input Format

The input contains one line with 5 integers: $a$, $b$, $k$, $n$, $m$. Each pair of consecutive integers is separated by a single space.

### Output Format

The output contains one integer representing the desired coefficient. Since the coefficient can be very large, output it modulo $10,007$.

### Example

### Constraints and Hints

- For $30\%$ of the test cases, $k \leq 10$.
- For $50\%$ of the test cases, $a = 1, b = 1$.
- For $100\%$ of the test cases, $0 \leq k \leq 1000$, $0 \leq n, m \leq k$, and $n + m = k$, $0 \leq a, b \leq 10^6$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

In the process of implementing programmatic automatic analysis, it is often necessary to determine whether certain constraints can be satisfied simultaneously.

Consider a simplified version of a constraint satisfaction problem: Suppose $x_1, x_2, x_3, \ldots$ represent variables appearing in a program. You are given $n$ constraints of the form $x_i = x_j$ or $x_i \neq x_j$. Your task is to decide whether it is possible to assign each variable an appropriate value such that all of the given constraints are satisfied simultaneously.

For example, consider the following constraints: $x_1 = x_2, \ x_2 = x_3, \ x_3 = x_4, \ x_1 \neq x_4$. These constraints are clearly contradictory and cannot all be satisfied at the same time. Therefore, this problem should be determined as unsatisfiable.

Now, you are given several such constraint satisfaction problems; for each of them, you need to determine whether they are satisfiable.

---

### Input Format

The first line of the input file contains a positive integer $t$, representing the number of problems to be evaluated. Note that these problems are independent of each other.

For each problem:
- The first line contains a positive integer $n$, representing the number of constraints for the problem.
- The next $n$ lines each contain three integers $i, j, e$, describing a constraint. Integers are separated by a single space. If $e = 1$, the constraint is $x_i = x_j$; if $e = 0$, the constraint is $x_i \neq x_j$.

---

### Output Format

The output file consists of $t$ lines.

The $k$-th line of the output should contain the string `YES` or `NO` (uppercase and without quotation marks):
- Output `YES` if the $k$-th problem is satisfiable.
- Output `NO` if the $k$-th problem is not satisfiable.

---

### Example

#### Input:
```
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
```

#### Output:
```
NO
YES
```

#### Explanation:
In the first problem, the constraints are $x_1 = x_2$ and $x_1 \neq x_2$. These two constraints are contradictory, so they cannot be satisfied simultaneously.
In the second problem, the constraints are $x_1 = x_2$ and $x_2 = x_1$. These two constraints are equivalent and can be satisfied simultaneously.

---

### Constraints and Hints

- For all test cases: $1 \leq t \leq 10$, $1 \leq n \leq 10^6$, $1 \leq i, j \leq 10^9$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Statement

On planet W, there are $n$ countries. To develop their economies, the countries have decided to construct bidirectional roads to ensure connectivity among them. However, the kings of these countries are very stingy and are only willing to build exactly $n - 1$ bidirectional roads.

The construction cost for each road is equal to the absolute difference between the number of countries at the two ends of the road multiplied by the road's length. For example, as shown in the figure below, the endpoints of the dashed road connect countries with $2$ and $4$ countries respectively. If the road has a length of $1$, its cost would be $1 \times \lvert 2 - 4 \rvert = 2$. In the figure, the numbers inside the circles represent the country indices.

![](https://i.loli.net/2018/06/11/5b1dfb1e69c02.png)

Due to the vast number of countries, there are numerous ways to construct the roads, and calculating the construction costs for each plan manually is infeasible. Hence, the kings decided to seek help designing a program that, given a specific construction plan, computes the total construction cost for it. Your task is to design such a program.

---

### Input Format

The first line of the input contains a single integer $n$, which represents the number of countries on planet W, numbered from $1$ to $n$.

The next $n - 1$ lines each describe the construction of one road. The $i$-th line contains three integers $a_i$, $b_i$, and $c_i$, indicating that the $i$-th bidirectional road connects country $a_i$ and country $b_i$, and its length is $c_i$.

---

### Output Format

Output a single integer representing the total cost required to construct all roads.

---

### Example

**Input**:
```
3
1 2 1
2 3 2
```

**Output**:
```
1
```

---

### Constraints and Hints

For all data, it is guaranteed that $1 \le a_i, b_i \le n$ and $0 \le c_i \le 10^6$.

| Test Case ID | $n=$ |
| :-: | :-: |
| 1 | $2$ |
| 2 | $10$ |
| 3 | $100$ |
| 4 | $200$ |
| 5 | $500$ |
| 6 | $600$ |
| 7 | $800$ |
| 8 | $1000$ |
| 9 | $10,000$ |
| 10 | $20,000$ |
| 11 | $50,000$ |
| 12 | $60,000$ |
| 13 | $80,000$ |
| 14 | $100,000$ |
| 15 | $600,000$ |
| 16 | $700,000$ |
| 17 | $800,000$ |
| 18 | $900,000$ |
| 19 | $1,000,000$ |
| 20 | $1,000,000$ |

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
Hanhang has two boxes of matches, each containing $n$ matches, and each match has a height. The matches in each box can be arranged into a row, where the heights of matches in the same row are distinct. The distance between the two rows of matches is defined as:
$$\sum_{i=1}^{n} (a_i - b_i)^2$$
where $a_i$ represents the height of the $i$-th match in the first row, and $b_i$ represents the height of the $i$-th match in the second row.

In each row, adjacent matches can be swapped. Your task is to minimize the distance between the two rows by swapping the positions of matches. Then find the minimal number of swaps required to achieve this minimum distance. **If the number of swaps is too large, output the result modulo 99,999,997.**

### Input Format
The input consists of three lines:
- The first line contains an integer $n$, representing the number of matches in each box.
- The second line contains $n$ integers separated by spaces, representing the heights of the matches in the first row.
- The third line contains $n$ integers separated by spaces, representing the heights of the matches in the second row.

### Output Format
Output a single integer representing the minimal number of swaps modulo 99,999,997 required to achieve the minimum distance.

### Example 1
For the input:
```
2
1 2
2 1
```
The minimum distance is $0$, and the minimal number of swaps is $1$. For example, swapping the first two matches in the first row or swapping the first two matches in the second row achieves the minimum distance.

### Example 2
For the input:
```
3
3 1 2
3 2 1
```
The minimum distance is $10$, and the minimal number of swaps is $2$. For example, swapping the middle two matches in the first row and swapping the last two matches in the second row achieves the minimum distance.

### Constraints and Hints
- For 10% of the test cases, $1 \leq n \leq 10$.
- For 30% of the test cases, $1 \leq n \leq 100$.
- For 60% of the test cases, $1 \leq n \leq 5,000$.
- For 100% of the test cases, $1 \leq n \leq 100,000$, and $0 \leq $ match height $ \leq 2^{31} - 1$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description

To prepare for a unique award ceremony, the organizers are laying rectangular carpets in a rectangular area of the venue (considered as the first quadrant of a Cartesian coordinate plane).

There are $n$ carpets, numbered from $1$ to $n$. These carpets are laid out in sequence according to their number, parallel to the coordinate axes. A newly placed carpet will cover earlier ones that have already been laid.

After all the carpets are placed, the organizers want to determine which carpet is at the top layer at a specific point on the ground.

Note: Points located on the boundaries and the four corners of a rectangular carpet are also considered to be covered.

### Input Format

The input consists of $n+2$ lines.

- The first line contains a single integer $n$, which represents the total number of carpets.
- The next $n$ lines describe the information of the carpets. The $(i+1)$-th line contains four positive integers: $a, b, g, k$, separated by a space. These integers describe the bottom-left corner $(a, b)$ of the carpet and its dimensions: $g$ is the length along the $x$-axis, and $k$ is the length along the $y$-axis.
- The last line ($n+2$-th line) contains two positive integers $x$ and $y$, which represent the coordinates of the point $(x, y)$ on the ground that is to be queried.

### Output Format

The output consists of a single line with one integer. The integer represents the carpet number of the topmost layer covering the queried point; if no carpet covers the point, output $-1$.

### Example 1

As shown below, the solid line represents Carpet 1, the dashed line represents Carpet 2, and the double solid line represents Carpet 3. The point $(2, 2)$ is covered by Carpet 3, which is the topmost layer.

![pic1.png](https://img.loj.ac.cn/2021/12/29/1b6a93302ed5e.png)

### Example 2

As shown above, the solid line represents Carpet 1, the dashed line represents Carpet 2, and the double solid line represents Carpet 3. The point $(4, 5)$ is not covered by any carpet, so the output is `-1`.

### Constraints and Hints

- For $30\%$ of the data, $n \leq 2$;
- For $50\%$ of the data, $a, b, g, k \leq 100$;
- For $100\%$ of the data: $0 \leq n \leq 10^4$, and $0 \leq a, b, g, k \leq 10^5$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this OJBench competition programming problem in Python. Read stdin and write stdout.

### Problem Description
In city S, there are currently two prisons, imprisoning a total of $N$ criminals numbered $1$ to $N$. Relationships among these criminals are naturally very discordant. Many of them bear grudges against each other, and conflicts may erupt at any time if the conditions are favorable. We use the "resentment value" (a positive integer) to represent the hatred level between any two criminals; the higher the resentment value $c$, the greater the grudge between the two criminals. If two criminals with resentment value $c$ are detained in the same prison, they will inevitably clash, causing a conflict with an impact level of $c$.

At the end of each year, the police department compiles a list of all the conflicts within the prison during the year, ordered from highest to lowest impact level, and submits it to Z, the mayor of city S. Busy with government affairs, Mayor Z only reviews the impact level of the first event on the list. If the impact is particularly high, he may consider replacing the police chief.

After studying the relationships among these $N$ criminals in detail, the police chief feels enormous pressure. He plans to reallocate the criminals between the two prisons to minimize the impact level of the largest conflict Mayor Z sees, thus safeguarding his position. Assuming that if two criminals in the same prison harbor resentment towards each other, conflicts will occur at some point during the year. How should the criminals be allocated to ensure Mayor Z sees the smallest conflict impact level? What is this minimum value?

---

### Input Format
Each line of the input separates two numbers by a space.

- The first line contains two positive integers $N$ and $M$, representing the number of criminals and the number of pairs of criminals with resentment, respectively.
- The next $M$ lines each contain three positive integers $a_j, b_j, c_j$, indicating that criminal $a_j$ and criminal $b_j$ harbor resentment with a resentment value of $c_j$. The data guarantees that $1 \leq a_j < b_j \leq N$, $0 < c_j \leq 1,000,000,000$, and each pair of criminals appears only once.

---

### Output Format
Output consists of $1$ line, representing the impact level of the conflict event seen by the mayor.

If no conflicts occur within the prisons during the year, output $0$.

---

### Example
The resentment values between criminals are shown in the left diagram below. The allocation of criminals is shown in the right diagram. The conflict event with the highest impact level seen by the mayor is $3512$ (triggered by criminals $2$ and $3$). Any other allocation would not yield a better result.

![298.png.jpg](https://i.loli.net/2018/05/22/5b03db0916ac8.jpg)

---

### Data Range and Hints
- For $30\%$ of the data, $N \leq 15$.
- For $70\%$ of the data, $N \leq 2,000$, $M \leq 50,000$.
- For $100\%$ of the data, $N \leq 20,000$, $M \leq 100,000$.

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
def main():
<Your code is here>

if __name__ == "__main__":
    main()```

### Answer: (use the provided format with backticks)


Solve this LiveCodeBench programming problem in Python. Read stdin and write stdout.

Title: Anti

Platform: atcoder

Difficulty: hard

Contest date: 2023-05-13

A DDoS-type string is a string of length 4 consisting of uppercase and lowercase English letters satisfying both of the following conditions.

- The first, second, and fourth characters are uppercase English letters, and the third character is a lowercase English letter.
- The first and second characters are equal.

For instance, DDoS and AAaA are DDoS-type strings, while neither ddos nor IPoE is.
You are given a string S consisting of uppercase and lowercase English letters and ?.
Let q be the number of occurrences of ? in S. There are 52^q strings that can be obtained by independently replacing each ? in S with an uppercase or lowercase English letter.
Among these strings, find the number of ones that do not contain a DDoS-type string as a subsequence, modulo 998244353.

Input

The input is given from Standard Input in the following format:
S

Output

Print the answer.

Constraints

- S consists of uppercase English letters, lowercase English letters, and ?.
- The length of S is between 4 and 3\times 10^5, inclusive.

Sample Input 1

DD??S

Sample Output 1

676

When at least one of the ?s is replaced with a lowercase English letter, the resulting string will contain a DDoS-type string as a subsequence.

Sample Input 2

????????????????????????????????????????

Sample Output 2

858572093

Find the count modulo 998244353.

Sample Input 3

?D??S

Sample Output 3

136604