#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define all(a) a.begin(), a.end()
#define db long double
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vvi vector<vi>

const int N = 1e5;
const int K = 20;
const int INF = 1e18;
const int mod = 998244353;
constexpr char nl = '\n';


void solve() {
    int n;
    cin >> n;
    vi a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }
    vi dp1(n, INT64_MIN);
    dp1[0] = a[1];
    int ans = dp1[0];
    vi p(n);
    bool m1 = 1;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        m1 &= p[i] == -1;
    }
    // if (!m1) return;
    for (int i = 2; i <= n; ++i) {
        vi dp2(n, INT64_MIN);
        int mx = INT64_MIN;
        for (int t = 0; t < i; ++t) {
            mx = max(mx, dp1[t]);
            dp2[t] = mx + a[i] + b[i] * t;
            ans = max(ans, dp2[t]);
        }
        dp1 = dp2;
    }
    cout << ans % mod;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
