#include <iostream>

using namespace std;

const long long MOD = 1000000007;
const long long P = 1243251;
const int N = 1 << 10;

long long t[N*4];

void treeSet(int a, int x, int c = 0, int cl = 0, int cr = N)
{
    if (cl + 1 == cr)
    {
        t[c] = x;
        return;
    }
    int mid = (cl + cr + 1) / 2;
    if (a < mid)
        treeSet(a, x, c*2+1, cl, mid);
    else
        treeSet(a, x, c*2+2, mid, cr);
    t[c] = t[c*2+1] + t[c*2+2];
}

long long treeSum(int l, int r, int c = 0, int cl = 0, int cr = N)
{
    if (cl >= r || l >= cr)
        return 0;
    if (l <= cl && cr <= r)
        return t[c];
    int mid = (cl + cr + 1) / 2;
    return treeSum(l, r, c*2+1, cl, mid) + treeSum(l, r, c*2+2, mid, cr);
}

int main()
{
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    int fa, fb;
    cin >> fa >> fb;
    int ans = 0;
    for (int i = 0; i < k; ++i)
    {
        if (i % 3 == 0)
            treeSet(fa - 1, fb);
        else
            ans = (ans * P + treeSum(min(fa, fb) - 1, max(fa, fb))) % MOD;
        fa = (1LL*fa*fa + fa*43 + fb) % 1000 + 1;
        fb = (1LL*fb*fb*9 + fb*23 + 12) % 1000 + 1;
    }
    cout << ans << "\n";
    return 0;
}
