Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem(a, h) memset(a, (h), sizeof(a))
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, ii> iii;
typedef pair<double, double> iidb;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9 + 7;
const double PI = acos(-1);
#define initseg ll new_nodo = (pnodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 3600000
#define offset 3000
#define ptr nodo *
const ll MOD = 1e9 + 7;

using namespace std;
const ll inf = 1e15;
int n;

int v[4][tam];
ll dp[4][tam];
ll go(int y) {
  int pslide = 0;
  ll tot = 0;

  forr(j, 0, n) {
    while (pslide < n && v[y - 1][pslide] < v[y][j]) {
      tot += dp[y - 1][pslide];
      if (y - 1 == 0)
        assert(dp[y - 1][pslide] == 1);
      pslide++;
    }
    dp[y][j] = tot;
  }
}
int main() {

  cin >> n;
  forr(i, 0, 3) {
    forr(j, 0, n) {
      if (i == 0)
        dp[i][j] = 1;
      cin >> v[i][j];
    }
    sort(v[i], v[i] + n);
  }
  ll tot = 0;
  go(1);
  go(2);
  forr(j, 0, n) tot += dp[2][j];
  cout << tot << endl;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem(a, h) memset(a, (h), sizeof(a))
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, ii> iii;
typedef pair<double, double> iidb;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9 + 7;
const double PI = acos(-1);
#define initseg ll new_nodo = (pnodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 3600000
#define offset 3000
#define ptr nodo *
const ll MOD = 1e9 + 7;

using namespace std;
const ll inf = 1e15;
int n;

int v[4][tam];
ll dp[4][tam];
ll go(int y) {
  int pslide = 0;
  ll tot = 0;

  forr(j, 0, n) {
    while (pslide < n && v[y - 1][pslide] < v[y][j]) {
      tot += dp[y - 1][pslide];
      if (y - 1 == 0)
        assert(dp[y - 1][pslide] == 1);
      pslide++;
    }
    dp[y][j] = tot;
  }
  return 0;
}
int main() {

  cin >> n;
  forr(i, 0, 3) {
    forr(j, 0, n) {
      if (i == 0)
        dp[i][j] = 1;
      cin >> v[i][j];
    }
    sort(v[i], v[i] + n);
  }
  ll tot = 0;
  go(1);
  go(2);
  forr(j, 0, n) tot += dp[2][j];
  cout << tot << endl;
}