#include <bits/stdc++.h>
using namespace std;
int Getint() {
  char c = getchar();
  int ret = 0, f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (isdigit(c)) ret = (ret << 1) + (ret << 3) + c - '0', c = getchar();
  return ret * f;
}
const int Maxn = 1000005;
int n, k, ret, fac[Maxn], inv[Maxn];
void Prepare() {
  fac[0] = inv[0] = inv[1] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = (long long)fac[i - 1] * i % 998244353;
  for (int i = 2; i <= n; ++i)
    inv[i] =
        ((long long)998244353 - 998244353 / i) * inv[998244353 % i] % 998244353;
  for (int i = 2; i <= n; ++i)
    inv[i] = (long long)inv[i] * inv[i - 1] % 998244353;
}
int QuickPow(int x, long long k) {
  int ret = 1, t = x;
  while (k) {
    if (k & 1) ret = (long long)ret * t % 998244353;
    t = (long long)t * t % 998244353;
    k >>= 1;
  }
  return ret;
}
int C(int x, int y) {
  return (long long)fac[x] * inv[x - y] % 998244353 * inv[y] % 998244353;
}
int main() {
  n = Getint(), k = 3;
  Prepare();
  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    int flag = (i & 1) ? 1 : -1;
    ret = (ret +
           (long long)QuickPow(k, i) * C(n, i) % 998244353 *
               QuickPow(k, ((long long)n - i) * n) % 998244353 * flag +
           998244353) %
          998244353;
  }
  ret = ret * 2 % 998244353;
  for (int i = 1; i <= n; ++i) {
    int flag = (i & 1) ? 1 : -1;
    int t = QuickPow(k, n - i);
    ret = (ret +
           (long long)C(n, i) * flag * k % 998244353 *
               (QuickPow(t - 1, n) - QuickPow(t, n)) % 998244353 +
           998244353) %
          998244353;
  }
  cout << ret;
}
