#include <iostream>

using namespace std;

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

long long t[N*2-1];

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

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

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<0,0,N>(fa - 1, fb);
        else
            ans = (ans * P + treeSum<0,0,N>(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;
}
