#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define test int tt; cin>>tt; while(tt--)
#define ff first
#define ss second
#define pb push_back

#define Mod 998244353

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;

    vector<ll int> v(n);

    for(auto &e: v)
        cin>>e;

    sort(v.begin(), v.end());

    ll int sum_suff[n]={0};

    ll int pre=0;
    for(int i=n-1;i>=0;i--)
    {
        sum_suff[i]+=pre+v[i];
        pre*=2;
        pre+=v[i];

        sum_suff[i]%=Mod;
        pre%=Mod;
    }

    ll int ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(v[i]*sum_suff[i])%Mod;
        ans%=Mod;
    }

    cout<<ans<<endl;
}

#include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define rrepitr(itr, mp) for(auto itr = mp.rbegin(); itr!=mp.rend(); ++itr)
#define ALL(a) (a).begin(),(a).end()

template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

using namespace std;
using ll  = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;

double angle2rad(double angle){return angle * (PI / 180.0);}
double rad2angle(double rad){return rad * (180.0 / PI);}
ll mod(ll a, ll m){return (a + m) % m;}

int main(){
    ll n;
    cin >> n;

    vector<ll> l(n), r(n);
    vector<char> lc(n), rc(n);
    rep(i, n){
        ll t;
        cin >> t >> l[i] >> r[i];
        if(t == 1){lc[i] = '[';rc[i] = '[';}
        if(t == 2){lc[i] = '[';rc[i] = '(';}
        if(t == 3){lc[i] = '(';rc[i] = '[';}
        if(t == 4){lc[i] = '(';rc[i] = '(';}
    }

    ll ans = 0;
    rep(i, n)for(ll j = i+1; j < n; ++j){
        if(l[i] > r[j])continue;
        if(r[i] < l[j])continue;

        if(l[i] == r[j] && lc[i] != rc[j] )continue;
        if(l[i] == r[j] && lc[i] == '(')continue;
        if(r[i] == l[j] && rc[i] != lc[j])continue;
        if(r[i] == l[j] && rc[i] =='(')continue;
        
        
        // cout << l[i] << " : " << r[i] << " , " << l[j] << " : " << r[j] << endl; 
        // cout << lc[i] << " : " << rc[i] << " , " << lc[j] << " : " << rc[j] << endl; 
        ans++;
    }

    cout << ans << endl;
}