Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

typedef long double ld;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

// Output
template <class T, class S>
std::ostream &operator<<(std::ostream &os, std::pair<T, S> p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    os << " [" << i << "]" << v[i];
    if (i % 10 == 9)
      os << std::endl;
  }
  // os << v[0]; for (ll i = 1; i < (ll)v.size(); i++){ os << " " << v[i]; }
  return os;
}

template <class T, class S>
std::ostream &operator<<(std::ostream &os, std::map<T, S> m) {
  ll i = 0;
  for (auto p : m) {
    os << " [" << i << "]" << p.first << "->" << p.second;
    i++;
  }
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<std::vector<T>> vv) {
  for (auto &vec : vv) {
    os << vec << std::endl;
  }
  os << "--------" << std::endl;
  return os;
}

void print() { std::cout << std::endl; }
template <typename H> void print(H &&head) { std::cout << head << std::endl; }
template <typename H, typename... T> void print(H &&head, T &&...tail) {
  std::cout << head << " ", print(std::forward<T>(tail)...);
}

int main() {
  ll MAX_A = 100;
  ll N, K;
  std::cin >> N >> K;
  std::vector<ll> va(N, 0LL);
  std::vector<ll> vunder(MAX_A + 2, 0LL);
  for (ll i = 0; i < N; i++) {
    std::cin >> va[i];
    vunder[va[i] + 1]++;
  }

  // この値未満のものが何個あるかを調べる
  for (ll i = 1; i <= MAX_A + 1; i++) {
    vunder[i] += vunder[i - 1];
  }

  ll ans = 0LL;
  for (ll i = 0; i < N; i++) {

    // 最初の数列では、自分の値未満だが転倒数にならないものがある
    ll cnt = vunder[va[i]];
    for (ll j = 0; j < i; j++) {
      if (va[j] < va[i]) {
        cnt--;
      }
    }
    ans = (ans + cnt * K) % MOD;

    // Σ を求める
    ll num = (K * (K - 1) / 2) % MOD;
    ll val = (vunder[va[i]] * num) % MOD;
    ans = (ans + val) % MOD;
  }

  print(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

typedef long double ld;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

// Output
template <class T, class S>
std::ostream &operator<<(std::ostream &os, std::pair<T, S> p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    os << " [" << i << "]" << v[i];
    if (i % 10 == 9)
      os << std::endl;
  }
  // os << v[0]; for (ll i = 1; i < (ll)v.size(); i++){ os << " " << v[i]; }
  return os;
}

template <class T, class S>
std::ostream &operator<<(std::ostream &os, std::map<T, S> m) {
  ll i = 0;
  for (auto p : m) {
    os << " [" << i << "]" << p.first << "->" << p.second;
    i++;
  }
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<std::vector<T>> vv) {
  for (auto &vec : vv) {
    os << vec << std::endl;
  }
  os << "--------" << std::endl;
  return os;
}

void print() { std::cout << std::endl; }
template <typename H> void print(H &&head) { std::cout << head << std::endl; }
template <typename H, typename... T> void print(H &&head, T &&...tail) {
  std::cout << head << " ", print(std::forward<T>(tail)...);
}

int main() {
  ll MAX_A = 2000;
  ll N, K;
  std::cin >> N >> K;
  std::vector<ll> va(N, 0LL);
  std::vector<ll> vunder(MAX_A + 2, 0LL);
  for (ll i = 0; i < N; i++) {
    std::cin >> va[i];
    vunder[va[i] + 1]++;
  }

  // この値未満のものが何個あるかを調べる
  for (ll i = 1; i <= MAX_A + 1; i++) {
    vunder[i] += vunder[i - 1];
  }

  ll ans = 0LL;
  for (ll i = 0; i < N; i++) {

    // 最初の数列では、自分の値未満だが転倒数にならないものがある
    ll cnt = vunder[va[i]];
    for (ll j = 0; j < i; j++) {
      if (va[j] < va[i]) {
        cnt--;
      }
    }
    ans = (ans + cnt * K) % MOD;

    // Σ を求める
    ll num = (K * (K - 1) / 2) % MOD;
    ll val = (vunder[va[i]] * num) % MOD;
    ans = (ans + val) % MOD;
  }

  print(ans);
  return 0;
}