Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w});
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

ll A[N];

ll s[61], u[61];

int main() {
  fast;

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int j = 0; j < n; j++) {
    for (ll i = 0; i < 61; i++) {
      if (A[j] & (1LL << i))
        s[i]++;
      else
        u[i]++;
    }
  }

  ll ans = 0;

  for (ll i = 0; i < 61; i++) {
    ans = (ans + (((s[i] * u[i]) % mod) * ((1LL << i) % mod)) % mod) % mod;
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w});
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 3e5 + 5;

ll A[N];

ll s[61], u[61];

int main() {
  fast;

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int j = 0; j < n; j++) {
    for (ll i = 0; i < 61; i++) {
      if (A[j] & (1LL << i))
        s[i]++;
      else
        u[i]++;
    }
  }

  ll ans = 0;

  for (ll i = 0; i < 61; i++) {
    ans = (ans + (((s[i] * u[i]) % mod) * ((1LL << i) % mod)) % mod) % mod;
  }

  cout << ans;

  return 0;
}