Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, n) for (int i = 0; i < (n); ++i)
#define for1n(i, n) for (int i = 1; i <= (n); ++i)
#define for1(i, n) for (int i = 1; i < (n); ++i)
#define forn(i, n) for (int i = 0; i <= (n); ++i)
#define forx(i, n, x) for (int i = (x); i < (n); ++i)

#define mod 1000000007LL

using namespace std;

class llm {
  long long l;
  explicit operator int() { return l; }
  explicit operator long() { return l; }

public:
  llm(long long m = 0LL) : l((m % mod >= 0) ? (m % mod) : (m % mod + mod)){};

  llm operator=(llm m) {
    l = m.l;
    return *this;
  }
  // llm operator=(int i)      {l = i % mod; return *this;}
  // llm operator=(long i)     {l = i % mod; return *this;}
  // llm operator=(long long i){l = i % mod; return *this;}

  llm operator+(llm m) { return llm(l + m.l); }
  llm operator+(int m) { return llm(l + (m % mod)); }
  llm operator+(long m) { return llm(l + (m % mod)); }
  llm operator+(long long m) { return llm(l + (m % mod)); }

  llm operator-(llm m) { return llm(l - m.l); }
  llm operator-(int m) { return llm(l - (m % mod)); }
  llm operator-(long m) { return llm(l - (m % mod)); }
  llm operator-(long long m) { return llm(l - (m % mod)); }

  // llm operator-(long long m){return llm(l - m);}
  // llm operator-(int m){return llm(l - m);}

  llm operator*(llm m) {
    assert(0);
    return llm(l * m.l);
  }
  llm operator*(int m) { return llm(l * (m % mod)); }
  llm operator*(long m) { return llm(l * (m % mod)); }
  llm operator*(long long m) { return llm(l * (m % mod)); }

  // llm operator/(llm m)      {return llm(l / m.l);}
  llm operator/(int m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }
  llm operator/(long m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }
  llm operator/(long long m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }

  // llm operator%(llm m)      {return llm(l % m.l);}
  llm operator%(int m) { return llm(l % m); }
  llm operator%(long m) { return llm(l % m); }
  llm operator%(long long m) { return llm(l % m); }

  long long operator++(void) { return l = (l + 1) % mod; }
  long long operator++(int) {
    long long tmp = l;
    ++(*this);
    return tmp;
  }

  long long operator--(void) { return l = (l - 1) % mod; }
  long long operator--(int) {
    long long tmp = l;
    --(*this);
    return tmp;
  }

  llm operator-() { return llm(-l); }
  llm operator+() { return llm(l); }

  operator long long() {
    // cout << "l: " << l << "\n";
    return l;
  }

  friend istream &operator>>(istream &stream, llm &m);

  // operator int(){return l;}
};

istream &operator>>(istream &stream, llm &m) {
  long long tmp;
  stream >> tmp;
  m.l = tmp % mod;
  return stream;
}

typedef long long ll;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;

int main(void) {

  int N;
  llm K;
  cin >> N >> K;
  int a[2000] = {};

  for0(i, N) { cin >> a[i]; }

  pair<llm, llm> p[2000];

  for0(i, N) {
    p[i].first = 0;  // all
    p[i].second = 0; // after
  }

  for0(i, N) {
    for0(j, N) {
      if (a[i] > a[j]) {
        ++p[i].first;
        if (i < j) {
          ++p[i].second;
        }
      }
    }
    // p[i].first%=mod;
    // p[i].second%=mod;
  }

  llm result = 0;

  for0(i, N) {
    result = (result + (p[i].second * K));
    result = ((p[i].first * (K * (K - 1) / 2)) + result);

    // result = (result + (p[i].second *K %mod)) %mod;
    // result = ((p[i].first  * ((K-1) *K/2 % mod)) %mod  + result) % mod;
  }

  cout << result;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, n) for (int i = 0; i < (n); ++i)
#define for1n(i, n) for (int i = 1; i <= (n); ++i)
#define for1(i, n) for (int i = 1; i < (n); ++i)
#define forn(i, n) for (int i = 0; i <= (n); ++i)
#define forx(i, n, x) for (int i = (x); i < (n); ++i)

#define mod 1000000007LL

using namespace std;

class llm {
  long long l;
  explicit operator int() { return l; }
  explicit operator long() { return l; }

public:
  llm(long long m = 0LL) : l((m % mod >= 0) ? (m % mod) : (m % mod + mod)){};

  llm operator=(llm m) {
    l = m.l;
    return *this;
  }
  // llm operator=(int i)      {l = i % mod; return *this;}
  // llm operator=(long i)     {l = i % mod; return *this;}
  // llm operator=(long long i){l = i % mod; return *this;}

  llm operator+(llm m) { return llm(l + m.l); }
  llm operator+(int m) { return llm(l + (m % mod)); }
  llm operator+(long m) { return llm(l + (m % mod)); }
  llm operator+(long long m) { return llm(l + (m % mod)); }

  llm operator-(llm m) { return llm(l - m.l); }
  llm operator-(int m) { return llm(l - (m % mod)); }
  llm operator-(long m) { return llm(l - (m % mod)); }
  llm operator-(long long m) { return llm(l - (m % mod)); }

  // llm operator-(long long m){return llm(l - m);}
  // llm operator-(int m){return llm(l - m);}

  long long operator*(llm m) { return l * m.l; }
  llm operator*(int m) { return llm(l * (m % mod)); }
  llm operator*(long m) { return llm(l * (m % mod)); }
  llm operator*(long long m) { return llm(l * (m % mod)); }

  // llm operator/(llm m)      {return llm(l / m.l);}
  llm operator/(int m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }
  llm operator/(long m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }
  llm operator/(long long m) {
    assert(m < mod && mod % m != 0);
    return llm(l / m);
  }

  // llm operator%(llm m)      {return llm(l % m.l);}
  llm operator%(int m) { return llm(l % m); }
  llm operator%(long m) { return llm(l % m); }
  llm operator%(long long m) { return llm(l % m); }

  long long operator++(void) { return l = (l + 1) % mod; }
  long long operator++(int) {
    long long tmp = l;
    ++(*this);
    return tmp;
  }

  long long operator--(void) { return l = (l - 1) % mod; }
  long long operator--(int) {
    long long tmp = l;
    --(*this);
    return tmp;
  }

  llm operator-() { return llm(-l); }
  llm operator+() { return llm(l); }

  operator long long() {
    // cout << "l: " << l << "\n";
    return l;
  }

  friend istream &operator>>(istream &stream, llm &m);

  // operator int(){return l;}
};

istream &operator>>(istream &stream, llm &m) {
  long long tmp;
  stream >> tmp;
  m.l = tmp % mod;
  return stream;
}

typedef long long ll;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;

int main(void) {

  int N;
  llm K;
  cin >> N >> K;
  int a[2000] = {};

  for0(i, N) { cin >> a[i]; }

  pair<llm, llm> p[2000];

  for0(i, N) {
    p[i].first = 0;  // all
    p[i].second = 0; // after
  }

  for0(i, N) {
    for0(j, N) {
      if (a[i] > a[j]) {
        ++p[i].first;
        if (i < j) {
          ++p[i].second;
        }
      }
    }
    // p[i].first%=mod;
    // p[i].second%=mod;
  }

  llm result = 0;

  for0(i, N) {
    result = (result + (p[i].second * K));
    result = ((p[i].first * (K * (K - 1) / 2)) + result);

    // result = (result + (p[i].second *K %mod)) %mod;
    // result = ((p[i].first  * ((K-1) *K/2 % mod)) %mod  + result) % mod;
  }

  cout << result;

  return 0;
}