#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
const int MOD = 998244353;
int n, fact[N], rfact[N];
void add(int &x, int y) {
  x += y;
  x -= (x >= MOD) * MOD;
}
void sub(int &x, int y) {
  x -= y;
  x += (x < 0) * MOD;
}
int powMod(int n, long long k) {
  assert(k >= 0);
  if (k == 0) return 1;
  int res = powMod(n, k >> 1);
  res = (long long)res * res % MOD;
  if (k & 1) res = (long long)res * n % MOD;
  return res;
}
void init() {
  fact[0] = 1;
  for (int i = 1; i <= n; i++) fact[i] = (long long)fact[i - 1] * i % MOD;
  rfact[n] = powMod(fact[n], MOD - 2);
  for (int i = n - 1; i >= 0; i--)
    rfact[i] = (long long)rfact[i + 1] * (i + 1) % MOD;
  assert(rfact[0] = 1);
}
int nCk(int n, int k) {
  return (long long)fact[n] * rfact[k] % MOD * rfact[n - k] % MOD;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  init();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int tmp =
        (long long)2 * nCk(n, i) * powMod(3, (long long)n * (n - i) + i) % MOD;
    if (i & 1)
      add(ans, tmp);
    else
      sub(ans, tmp);
  }
  for (int i = 0; i <= n - 1; i++) {
    int pw3 = (MOD - powMod(3, i)) % MOD;
    int tmp = (long long)3 * nCk(n, i) *
              ((powMod((1 + pw3) % MOD, n) - powMod(pw3, n) + MOD) % MOD) % MOD;
    if (i & 1)
      add(ans, tmp);
    else
      sub(ans, tmp);
  }
  cout << ans;
  return 0;
}
