#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353, maxn = 1e6 + 10;
int n;
long long fac[maxn], inv[maxn];
long long qmul(long long base, long long pow) {
  long long ret = 1;
  for (; pow; pow >>= 1) {
    if (pow & 1) ret = ret * base % mod;
    base = base * base % mod;
  }
  return ret;
}
long long calc(long long x, long long y) {
  return fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
  cin >> n;
  long long ans = 0;
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = qmul(fac[i], mod - 2);
  }
  for (int i = 1; i <= n; i++)
    ans =
        (ans +
         qmul(3, (1ll * n * (n - i) + i)) * qmul(-1, i + 1) * calc(n, i) % mod +
         mod) %
        mod;
  ans = ans * 2 % mod;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    int t = -qmul(3, i);
    tmp = (tmp +
           calc(n, i) * qmul(-1, i + 1) * (qmul(1 + t, n) - qmul(t, n) + mod) %
               mod +
           mod) %
          mod;
  }
  ans = (ans + tmp * 3) % mod;
  cout << ans << endl;
  return 0;
}
