TnT/CodeNet4Repair huggingface dataset:
Problem:
We have a tree with N vertices numbered 1 to N. The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i. Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:    * For any two vertices painted in the same color, the distance between them is an even number.  Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Incorrect submission (verdict: Wrong Answer):
n = int( input())

l = [[[] for _ in range( 2 )] for _ in range( n )]

for i in range( n - 1 ):
  u, v, w = map( int, input().split())
  l[ u - 1 ][ w % 2 ].append( v - 1 )

ret = [1] + [0] * ( n - 1 )

def check( i ):
  k = l[ i ][ 0 ]
  for j in k:
    ret[ j ] = 1
    check( j )

check( 0 )

for i in ret:
  print( i )

Fixed, accepted submission:
N = int( input() )
d = [ [] for _ in range( N )]

for i in range( N - 1 ):
  v, u, w = map( int, input().split() )
  d[ v - 1 ].append(( u - 1, w ))
  d[ u - 1 ].append(( v - 1, w ))

s = [( 0, 0 )]
l = [ -1 ] * N
while s != []:
  a, w = s.pop()
  l[ a ] = w % 2
  for i, j in d[ a ]:
    if l[ i ] == -1:
      s.append( ( i , j + w ) )
for i in l:
  print( i )
Problem:
Sitting in a station waiting room, Joisino is gazing at her train ticket.  The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).  In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with `+` or `-` so that the formula holds.  The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Incorrect submission (verdict: Wrong Answer):
A=list(input())
for i in range(4):
  A[i]=int(A[i])

for bit in range(8):
  flag=["+","+","+"]
  for i in range(3):
    if not bit>>(2-i) & 1 :
      A[i+1]=-A[i+1]
      flag[i]="-"
  if sum(A)==7:
    print("{}{}{}{}{}{}{}".format(A[0],flag[0],A[1],flag[1],A[2],flag[2],A[3]))
    exit()
  for i in range(4):
    A[i]=abs(A[i])

Fixed, accepted submission:
s=input()
ans=0
def dfs(i,stri,sum): 
    global ans
    #print(i,stri,sum)
    if i==3 and sum==7:
        #print("ore")
        ans=stri+"=7"
        
    
    if i<=2:
        dfs(i+1, stri+ f"+{s[i+1]}" , sum+int(s[i+1]))
        dfs(i+1, stri+ f"-{s[i+1]}", sum-int(s[i+1]))

dfs(0,s[0],int(s[0]))
print(ans)
Problem:
You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan). In how many ways can we select some of these coins so that they are X yen in total?  Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Incorrect submission (verdict: Wrong Answer):
A = int(input())
B = int(input())
C = int(input())
X = int(input())

import itertools
len([c for c in set(map(tuple, itertools.chain.from_iterable(itertools.combinations(itertools.chain(itertools.repeat(500, A), itertools.repeat(100, B), itertools.repeat(50, C)), r) for r in range(A + B + C + 1)))) if sum(c) == X])

Fixed, accepted submission:
A = int(input())
B = int(input())
C = int(input())
X = int(input())

import itertools
print(len([total for total in map(sum, itertools.product(
    [500 * i for i in range(A + 1)],
    [100 * i for i in range(B + 1)],
    [50 * i for i in range(C + 1)],
)) if total == X]))
Problem:
Write a program which reads a sequence of $n$ integers $a_i (i = 1, 2, ... n)$, and prints the minimum value, maximum value and sum of the sequence.

Incorrect submission (verdict: Wrong Answer):
n = int(input())
a = input().split()

int_a = []
for i in range(n):
    int_a.append(int(a[i]))

print("{} {} {}".format(max(int_a), min(int_a), sum(int_a)))



Fixed, accepted submission:
n = int(input())
a = input().split()

int_a = []
for i in range(n):
    int_a.append(int(a[i]))

print("{} {} {}".format(min(int_a), max(int_a), sum(int_a)))
Problem:
You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Incorrect submission (verdict: Wrong Answer):
s = input()
ss = "".join(set(s))
print("Yes") if len(s) == len(ss) else print("No")

Fixed, accepted submission:
s = input()
ss = "".join(set(s))
print("yes") if len(s) == len(ss) else print("no")
Problem:
Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.  She will concatenate all of the strings in some order, to produce a long string.  Among all strings that she can produce in this way, find the lexicographically smallest one.  Here, a string s=s_1s_2s_3...s_n is _lexicographically smaller_ than another string t=t_1t_2t_3...t_m if and only if one of the following holds:    * There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.   * s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Incorrect submission (verdict: Wrong Answer):
n, l = map(int,input().split())
s = []
for i in range(n):
    s.append(input())
print(s)
s.sort()
print(s)
print("".join(s))


Fixed, accepted submission:
n, l = map(int,input().split())
s = []
for i in range(n):
    s.append(input())
#print(s)
s.sort()
#print(s)
print("".join(s))
Problem:
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = `E`, and west if S_i = `W`.  You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing.  The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions.

Incorrect submission (verdict: Wrong Answer):
N = int(input())
S = input()
min_num = 3e5
for i in range(N):
    num = S[:i].count('W') + S[i:].count('E')
    min_num = num if min_num > num else min_num
    print(i, num)
print(min_num)

Fixed, accepted submission:
N = int(input())
S = input()
num = S.count('E')
min_num = num
for i in range(N-1):
    if S[i]=='W':
        num += 1
    if S[i+1]=='E':
        num -= 1
    min_num = num if min_num > num else min_num
print(min_num if S[0]=='W' else min_num-1)
Problem:
"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.  Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Incorrect submission (verdict: Wrong Answer):
A,B,C,X,Y = map(int, input().split())

if A+B < 2*C:
    print(A*X+B*Y)
else:    
    ans = float('inf')
    for i in range(0,max(X,Y)*2+1,2):
        ans = min(ans, max((X-i//2), 0)+max(B*(Y-i//2),0) + C*i)
    print(ans)

Fixed, accepted submission:
def solve():
    A,B,C,X,Y = map(int, input().split())
    ans = X*A + Y*B
    for i in range(max(X,Y)+1):
        aandb = A * max(0, X-i) + B * max(0, Y-i)
        ab = 2*C*i
        ans = min(ans, aandb + ab)
    print(ans)

if __name__ == '__main__':
    solve()
Problem:
N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.  The person numbered i says:    * "In front of me, exactly A_i people are wearing hats with the same color as mine."  Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.  Since the count can be enormous, compute it modulo 1000000007.

Incorrect submission (verdict: Wrong Answer):
n=int(input())
a=list(map(int,input().split()))
a_=max(a)
con=[0]*(a_+1)
tou=[0]*(a_+1)
ans=[]
for i in range(len(a)):
    u=a[i]
    if u==0:
        con[0]+=1
        tou[0]+=1
        ans.append(4-con[0])

    else:
        con[u]+=1 
        tou[u]+=1  
        ans.append(min(tou[u-1],4-con[u]))
c=1
for i in range(len(ans)):
    c*=ans[i]
print(c%(10**9+7))   
print(ans) 

Fixed, accepted submission:
n=int(input())
a=list(map(int,input().split()))
a_=max(a)
con=[0]*(a_+1)
tou=[0]*(a_+1)
ans=[]
for i in range(len(a)):
    u=a[i]
    if u==0:
        con[0]+=1
        tou[0]+=1
        ans.append(4-con[0])

    else:
        con[u]+=1 
        tou[u]+=1  
        ans.append(tou[u-1]-con[u]+1)
c=1
for i in range(len(ans)):
    c*=ans[i]
    c=c%(10**9+7)
print(c%(10**9+7))
Problem:
You are given a string S of length N consisting of lowercase English letters, and an integer K. Print the string obtained by replacing every character in S that differs from the K-th character of S, with `*`.

Incorrect submission (verdict: Wrong Answer):
n=int(input())
s=list(input())
k=int(input())
for i in range(n):
    if s[i] != s[k-1]:
        s[i]=s[k-1]
print(''.join(s))


Fixed, accepted submission:
n=int(input())
s=list(input())
k=int(input())
for i in range(n):
    if s[i] != s[k-1]:
        s[i]="*"
print(''.join(s))
Problem:
Takahashi loves numbers divisible by 2.  You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.  Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.  For example,    * 6 can be divided by 2 once: 6 -> 3.   * 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.   * 3 can be divided by 2 zero times.

Incorrect submission (verdict: Wrong Answer):
n = int(input())
x = [i for i in range(1,n+1)]
res = []
judge = 0

while len(x)>1:
  for j in x:
    if j%2==0:
      res.append(j/2)
  else:
    x = res
    res = []
    judge += 1

print(x[0]*(2**judge))
    

Fixed, accepted submission:
import math
n = int(input())
x = [i for i in range(1,n+1)]
res = []
judge = 0

while len(x)>1:
  for j in x:
    if j%2==0:
      res.append(j/2)
  else:
    x = res
    res = []
    judge += 1

print(math.floor(x[0]*(2**judge)))
Problem:
Write a program which reads two integers a and b, and calculates the following values:    * a ÷ b: d (in integer)   * remainder of a ÷ b: r (in integer)   * a ÷ b: f (in real number)

Incorrect submission (verdict: Wrong Answer):
x = input().split(" ")
a = int(x[0])
b = int(x[1])
d = int(a/b)
r = int(a%b)
f = a/b
print(str(d) + " " + str(r) + " " + str(f))

Fixed, accepted submission:
a,b = map(int, input().split())
print("%d %d %.5f" % (int(a/b),a%b,a/b))
Problem:
Takahashi loves numbers divisible by 2.  You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.  Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.  For example,    * 6 can be divided by 2 once: 6 -> 3.   * 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.   * 3 can be divided by 2 zero times.

Incorrect submission (verdict: Wrong Answer):
N = int(input())
ans = 0
for i in range(1, N+1):
    i_ans = 0
    while i % 2:
        i_ans += 1
        i = i//2
    ans = max(ans, i_ans)
print(pow(2, ans))


Fixed, accepted submission:
N = int(input())
ans = 0
for i in range(1, N+1):
    i_ans = 0
    while i % 2 == 0:
        i_ans += 1
        i = i//2
    ans = max(ans, i_ans)
print(pow(2, ans))
Problem:
We say that a odd number N is _similar to 2017_ when both N and (N+1)/2 are prime.  You are given Q queries.  In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Incorrect submission (verdict: Time Limit Exceeded):
import math
isPrime = [True for i in range(10**5+1)]
for i in range(3, 10**5+1):
    for j in range(2, math.floor(math.sqrt(i))+1):
        if i % j == 0:
            isPrime[i] = False

cntPrime = [0 for i in range(10**5+1)]
for i in range(2,10**5+1):
    if i % 2 == 1 and isPrime[i] and isPrime[(i+1)//2]:
        cntPrime[i] = cntPrime[i-1]+1
    else:
        cntPrime[i] = cntPrime[i-1]

Q = int(input())
for i in range(Q):
    l, r = [int(_) for _ in input().split()]
    print(cntPrime[r]-cntPrime[l-1])


Fixed, accepted submission:
import math
prime = [i for i in range(2, 10**5+1)]
for i in range(2, math.floor(math.sqrt(10**5))+1):
    prime = [p for p in prime if (p == i or p % i != 0)]
isPrime = [False for i in range(10**5+1)]
for p in prime:
    isPrime[p] = True
cntPrime = [0 for i in range(10**5+1)]
for i in range(2, 10**5+1):
    if i % 2 == 1 and isPrime[i] and isPrime[(i+1)//2]:
        cntPrime[i] = cntPrime[i-1]+1
    else:
        cntPrime[i] = cntPrime[i-1]

Q = int(input())
for i in range(Q):
    l, r = [int(_) for _ in input().split()]
    print(cntPrime[r]-cntPrime[l-1])
Problem:
You went shopping to buy cakes and donuts with X yen (the currency of Japan).  First, you bought one cake for A yen at a cake shop. Then, you bought as many donuts as possible for B yen each, at a donut shop.  How much do you have left after shopping?

Incorrect submission (verdict: Wrong Answer):
X=input()
A=input()
B=input()
k = int(X) - int(A)
k % int(B)

Fixed, accepted submission:
X=input()
A=input()
B=input()
k = int(X) - int(A)
print(k % int(B))
Problem:
Ringo is giving a present to Snuke.  Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes grilled things starting with `YAKI` in Japanese, and does not like other things.  You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with `YAKI`.

Incorrect submission (verdict: Wrong Answer):
s = input()
if s.startswith('YAKI'):
    print('YES')
else:
    print('NO')


Fixed, accepted submission:
s = input()
if s.startswith('YAKI'):
    print('Yes')
else:
    print('No')
Problem:
One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N. He is interested in properties of the sequence a.  For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its _beauty_ is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)  Find the maximum possible value for him.

Incorrect submission (verdict: Wrong Answer):
from bisect import bisect_left as bl
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    c=0
    for j in a[i:]:
        c+=j
        l.append(c)
t=0
for i in range(40,0,-1):
    s=t+2**i
    c=0
    for j in l[bl(l,t):]:
        if j&s==s:
            c+=1
    else:
        if k<=c:
            t=s
print(t)

Fixed, accepted submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    c=0
    for j in a[i:]:
        c+=j
        l.append(c)
t=0
for i in range(40,-1,-1):
    s=2**i
    b=[u for u in l if s&u!=0]
    if len(b)>=k:
        l=b
        t+=s
print(t)
Problem:
AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c. Here, the color of each paint can is represented by an integer between 1 and 100, inclusive.  Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him.

Incorrect submission (verdict: Wrong Answer):
a,b,c = map(int,input().split())

l = [0] * 3

l[0] = a
l[1] = b
l[2] = c

k = [0] * 3

k[0] = l.count(a)
k[1] = l.count(b)
k[2] = l.count(c)

print(max(k))



Fixed, accepted submission:
a,b,c = map(int,input().split())

l = [0] * 3

l[0] = a
l[1] = b
l[2] = c

k = [0] * 3

k[0] = l.count(a)
k[1] = l.count(b)
k[2] = l.count(c)

t = max(k)

if t == 1:
	t = 3
elif t ==3:
	t = 1
else:
	pass


print(t)
Problem:
We have a grid with H rows and W columns, where all the squares are initially white.  You will perform some number of painting operations on the grid. In one operation, you can do one of the following two actions:    * Choose one row, then paint all the squares in that row black.   * Choose one column, then paint all the squares in that column black.  At least how many operations do you need in order to have N or more black squares in the grid? It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Incorrect submission (verdict: Wrong Answer):
a = [int(input()) for i in range(2)]
b = int(input())
c = b // max(a)
print(c)

Fixed, accepted submission:
a = [int(input()) for i in range(2)]
b = int(input())
if b % max(a) == 0:
  print(b // max(a))
else:
  print(b // max(a) + 1)
Problem:
There are N participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.  Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.    * A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B.   * An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B and the student ranks B-th or above among all overseas students.  A string S is assigned indicating attributes of all participants. If the i-th character of string S is `a`, this means the participant ranked i-th in the Qualification contests is a Japanese student; `b` means the participant ranked i-th is an overseas student; and `c` means the participant ranked i-th is neither of these.  Write a program that outputs for all the participants in descending rank either `Yes` if they passed the Qualification contests or `No` if they did not pass.

Incorrect submission (verdict: Wrong Answer):
n,A,B=map(int,input().split())
contestant=list(input())
aa=0
bb=0
for i in range(n):
  if contestant[i]=="c":
    print("No")
  elif contestant[i]=="b":
    if aa+bb<A+B and bb<B:
      print("Yes")
    else:
      print("No")
    bb+=1
    
  elif contestant[i]=="a":
    if aa+bb<A+B+1 :
      print("Yes")
    else:
      print("No")
    aa+=1

Fixed, accepted submission:
n,A,B=map(int,input().split())
contestant=list(input())
aa=0
bb=0
for i in range(n):
  if contestant[i]=="c":
    print("No")
  elif contestant[i]=="b":
    if aa+bb<A+B and bb<B:
      print("Yes")
      bb+=1
    else:
      print("No")
    
  elif contestant[i]=="a":
    if aa+bb<A+B:
      print("Yes")
    else:
      print("No")
    aa+=1
Problem:
Kenkoooo is planning a trip in Republic of Snuke. In this country, there are n cities and m trains running. The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally. Any city can be reached from any city by changing trains.  Two currencies are used in the country: yen and snuuk. Any train fare can be paid by both yen and snuuk. The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.  In a city with a money exchange office, you can change 1 yen into 1 snuuk. However, when you do a money exchange, you have to change all your yen into snuuk. That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk. Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.  Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling. It is acceptable to do the exchange in City s or City t.  Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange. For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years. You can assume that the trip finishes within the year.

Incorrect submission (verdict: Wrong Answer):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

N,M,S,T = map(int,input().split())
UVAB = [[int(x) for x in input().split()] for _ in range(M)]
U,V,A,B = zip(*UVAB)

g1 = csr_matrix((A,(U,V)), (N+1,N+1))
g2 = csr_matrix((B,(U,V)), (N+1,N+1))
d1 = dijkstra(g1, indices=S, directed=False)
d2 = dijkstra(g2, indices=T, directed=False)

d = d1 + d2 

d = (np.minimum.accumulate(d[::-1])[::-1]).astype(int)

answer = 10 ** 10 - d[1:]
print(*answer, sep='\n')

Fixed, accepted submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np
from heapq import heappush, heappop

N,M,S,T = map(int,input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
    u,v,a,b = map(int,input().split())
    graph[u].append((v,a,b))
    graph[v].append((u,a,b))

def dijkstra(start, mode):
    INF = 10 ** 15
    dist = [INF] * (N+1)
    dist[start] = 0
    q = [(0,start)]
    while q:
        d,v = heappop(q)
        if dist[v] < d:
            continue
        for w,*a in graph[v]:
            d1 = d + a[mode]
            if dist[w] > d1:
                dist[w] = d1
                heappush(q, (d1,w))
    return dist

d1 = np.array(dijkstra(S,0))
d2 = np.array(dijkstra(T,1))

d1 += d2
answer = 10 ** 15 - np.minimum.accumulate(d1[::-1])[::-1][1:]
print('\n'.join(answer.astype(str)))
Problem:
You are given a string S consisting of lowercase English letters. Another string T is initially empty. Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:    * Append one of the following at the end of T: `dream`, `dreamer`, `erase` and `eraser`.

Incorrect submission (verdict: Wrong Answer):
s=input()
s=s.replace("eraser","")
s=s.replace("erase","")
s=s.replace("dreamer","")
s=s.replace("drea,","")
if s:
    print("NO")
else:
    print("YES")

Fixed, accepted submission:
s=input()
s=s.replace("eraser","")
s=s.replace("erase","")
s=s.replace("dreamer","")
s=s.replace("dream","")
if s:
    print("NO")
else:
    print("YES")
Problem:
We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.  Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.  Find the inversion number of B, modulo 10^9 + 7.  Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Incorrect submission (verdict: Wrong Answer):
n,k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7

cnt = 0
for i in range(n-1):
    for j in range(i+1,n):
        if a[i] > a[j]: cnt += 1

y = 0
for a1 in a:
    for a2 in a:
        print(a1,a2)
        if a1 > a2: y += 1

ans = (cnt * k + y * k * (k-1)//2)%MOD
print(ans)

Fixed, accepted submission:
n,k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7

cnt = 0
for i in range(n-1):
    for j in range(i+1, n):
        if a[i] > a[j]: cnt += 1
y = 0
for a1 in a:
    for a2 in a:
        if a1 > a2: y += 1

ans = (cnt*k + y*k*(k-1)//2)%MOD
print(ans)